Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress循环-如何计数项目_Wordpress_Loops_Count - Fatal编程技术网

Wordpress循环-如何计数项目

Wordpress循环-如何计数项目,wordpress,loops,count,Wordpress,Loops,Count,有没有办法在Wordpress循环代码中获取大量项目: <?php while (have_posts()) : the_post(); ?> 这个循环列出了帖子。我需要根据类的总数将某些类添加到前3个中。您可以使用of$WP\u查询,如下所示: $wp_query->post_count 注意found_posts的区别,它统计虽然与查询匹配但未显示的posts(例如用于分页)。根据您的具体情况,您可能需要使用其中一种方法。这里有一种方法: <?php $c

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

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

这个循环列出了帖子。我需要根据类的总数将某些类添加到前3个中。

您可以使用of
$WP\u查询,如下所示:

$wp_query->post_count

注意
found_posts
的区别,它统计虽然与查询匹配但未显示的posts(例如用于分页)。根据您的具体情况,您可能需要使用其中一种方法。

这里有一种方法:

<?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;
 ?>

我在我的电脑里用过这个

<?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; ?>


OP需要循环中的帖子总数,而不是当前的索引。@Sunyatasattva“我需要在前3个帖子中添加某些类”-他需要计算并测试少于4个帖子才能添加特殊类。。。否决票?真的吗?就在引用的句子“取决于它们的总数”之后。他不需要将类添加到前三个中,这些类将取决于总数。我投了反对票,因为这不能回答问题。如果你编辑答案以真正解决OP问题,那么否决票总是可以收回的。@Sunyatasattva显然你在理解循环和计数器方面有问题,而我甚至不阅读php。OP询问循环发生时的项数。当前索引表示循环发生的次数,因此给出了计数。@sksallaj我知道什么是循环,什么是计数器,我相信你误读了OP问题,根据你的第二条评论,你不知道Wordpress。OP将决定谁误解了。在任何情况下:如果您只是想要一个计数器,Wordpress
$WP\u Query
提供了方便的选项,可以为您提供当前的循环索引。谢谢你们的回答。这个更接近我需要的。也许我不够精确。我需要的物品数量,无论有多少。然后我需要将类添加到前3个或更少的类中,如果您愿意的话,您也可以使用
$items=count($posts)
:)当场
found_posts
是适合我的。谢谢