0

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 
4

2 回答 2

2

您需要做的只是将您的代码段包装到if语句中并在循环the_post之前调用函数。while通过调用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;
于 2013-01-27T11:58:57.257 回答
0

根据你想要的样式[整篇文章、标题等],你会想要识别第一篇文章,然后有条件地输出一个类或你想要的任何 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 
}
于 2014-11-16T10:36:07.140 回答