Wordpress循环 - 如何计算项目

时间:2013-10-10 18:34:23

标签: wordpress loops count

有没有办法在Wordpress循环代码中获取大量项目:

<?php while (have_posts()) : the_post(); ?>

此循环列出帖子。 我需要将某些类添加到前3个,具体取决于它们的总数。

3 个答案:

答案 0 :(得分:23)

您可以使用$WP_Query的{​​{3}},如下所示:

$wp_query->post_count

请注意与found_posts的区别,{{1}}计算的帖子虽然与查询匹配,但未显示(例如,用于分页)。您可能希望根据您的具体情况使用其中一种。

答案 1 :(得分:13)

这是一种方法:

<?php 
 $count = 0; //set up counter variable
 while (have_posts()) : the_post(); 
 $count++; //increment the variable by 1 each time the loop executes
 if ($count<4) {
    // here put the special code for first three
 }
 // here put the code for normal posts
 endwhile;
 ?>

答案 2 :(得分:0)

我在我的身上用过

<?php $count = 0;
  if ( have_posts() ) : while ( have_posts() ) : the_post(); $count++;?>
        <div  class="col-lg-3">
            <h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
            <p><?php the_excerpt();?></p>
        </div>

<?php if ($count==4) { $count = 0;?>
        <div class="clearfix"></div>
<?php } ?>

<?php endwhile; endif; ?>
相关问题