我正在使用query_post显示最近发布的列表。我想为第一篇文章提供特殊的样式和html标记。
这是我当前的代码:
$cat_args=array( 'orderby' => 'name', 'order' => 'ASC' ); $categories=get_categories($cat_args); foreach($categories as $category) { $args=array( 'showposts' => -1, 'category__in' => array($category->term_id), 'caller_get_posts'=>1 ); $posts=query_posts($args); if ($posts) { echo '<h3><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </h3> '; while ( have_posts() ) : the_post(); if( $wp_query->current_post == 0 ) :?> <?php if ( has_post_thumbnail()) : ?> <a href="<?php the_permalink(); ?>" class="thumb" title="<?php the_title_attribute(); ?>" ><?php the_post_thumbnail('post-thumb'); ?></a> <?php endif; ?> <h5><a href="<?php the_permalink(); ?>"> <?php the_title(); ?></a></h5> <?php else : ?> <?php if ( has_post_thumbnail()) : ?> <a href="<?php the_permalink(); ?>" class="thumb" title="<?php the_title_attribute(); ?>" ><?php the_post_thumbnail('post-thumb'); ?></a> <?php endif; ?> <h5><a href="<?php the_permalink(); ?>"> <?php the_title(); ?></a></h5> <?php endif; endwhile; } // if ($posts } // foreach($categories #1
你所需要做的就是将代码段包装到if语句中, 并在while循环之前调用the_post函数。通过调用the_post函数, 你将从队列中获取第一条记录。应该是这样的:
if ( have_posts() ) : the_post(); if ( has_post_thumbnail()) : ?><a href="<?php the_permalink(); ?>" class="thumb" title="<?php the_title_attribute(); ?>" > <?php the_post_thumbnail('post-thumb'); ?> </a><?php endif; ?><h5> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> </h5><?php while ( have_posts() ) : the_post(); if ( has_post_thumbnail()) : ?> <a href="<?php the_permalink(); ?>" class="thumb" title="<?php the_title_attribute(); ?>" ><?php the_post_thumbnail('post-thumb'); ?></a> <?php endif; ?> <h5><a href="<?php the_permalink(); ?>"> <?php the_title(); ?></a></h5> endwhile; endif; #2
根据你要样式化的内容(整个文章, 标题等等), 你将要确定第一篇文章, 然后有条件地输出一个类或该文章想要的任何HTML。 “循环”是while和endwhile之间的代码部分。因此, 在循环之前, 输入:
$is_first_post = TRUE; 然后在while循环中, 在尝试添加样式之前, 放入:
if ($is_first_post == TRUE){ echo (" .... THIS WOULD BE ADDED TO THE FIRST POST ONLY ...."); $is_first_post = FALSE; //this flags the next post as not being first }
srcmini
评论前必须登录!
注册