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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.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 - Fatal编程技术网

在WordPress循环中,我如何检查有多少帖子被循环通过?

在WordPress循环中,我如何检查有多少帖子被循环通过?,wordpress,loops,Wordpress,Loops,我的目标是在每四篇文章之后打印一些额外的html 以下代码有什么问题 <?php while ( have_posts() ) : the_post(); ?> <?php wc_get_template_part( 'content', 'product' ); ?> <?php $posts = $wp_query->post_count; if ( $posts == 4 ) {

我的目标是在每四篇文章之后打印一些额外的html

以下代码有什么问题

<?php while ( have_posts() ) : the_post(); ?>
    <?php wc_get_template_part( 'content', 'product' ); ?>
    <?php
        $posts = $wp_query->post_count;

        if ( $posts == 4 ) {
            echo '<div class="clearfix"></div>';
        }
    ?>
<?php endwhile; // end of the loop. ?>

以的基本代码为例,您可以包括一个计数器,在每次迭代中递增它,并检查其是否为零

<?php if ( have_posts() ) : $count = 1; ?>
    <?php while ( have_posts() ) : the_post();?>    
    <!-- do stuff ... -->
    <?php if ( $count % 4 == 0 ): ?>
        <!-- extra stuff every four posts -->
    <?php endif; ?>
    <?php $count++; endwhile; ?>
<?php endif; ?>

以的基本代码为例,您可以包括一个计数器,在每次迭代中递增它,并检查其是否为零

<?php if ( have_posts() ) : $count = 1; ?>
    <?php while ( have_posts() ) : the_post();?>    
    <!-- do stuff ... -->
    <?php if ( $count % 4 == 0 ): ?>
        <!-- extra stuff every four posts -->
    <?php endif; ?>
    <?php $count++; endwhile; ?>
<?php endif; ?>

试试这个

<?php 
$count = 1;
while ( have_posts() ) : the_post(); 
    wc_get_template_part( 'content', 'product' ); 
    if ( $count%4==0 ) {
        echo '<div class="clearfix"></div>';
    }
    $count++;
endwhile;  ?>

试试这个

<?php 
$count = 1;
while ( have_posts() ) : the_post(); 
    wc_get_template_part( 'content', 'product' ); 
    if ( $count%4==0 ) {
        echo '<div class="clearfix"></div>';
    }
    $count++;
endwhile;  ?>

在我之前的答案中可能有一个错误:它在每四篇文章的末尾重复“closing”标记-但是如果文章计数为5会怎么样?(或者任何其他不能被4整除的数字?当然,解决这个问题的需要可能因HTML而异。)

请看以下5个示例:

--帖子1--
--帖子2--
--第3篇--
--第4篇--
--结束标记--
--第5篇--

您可以看到,在第5次post之后,循环没有使用结束标记结束

因此,我建议使用此代码(考虑帖子数量或每页帖子数量):


在我之前的答案中可能有一个错误:它在每四篇文章的末尾重复“closing”标记-但是如果文章计数是5会怎么样?(或者任何其他不能被4整除的数字?当然,解决这个问题的需要可能因HTML而异。)

请看以下5个示例:

--帖子1--
--帖子2--
--第3篇--
--第4篇--
--结束标记--
--第5篇--

您可以看到,在第5次post之后,循环没有使用结束标记结束

因此,我建议使用此代码(考虑帖子数量或每页帖子数量):


我知道这个问题已经得到了回答,但我认为有更干净的方法可以做到这一点

您可以使用WP_查询对象中的几个有用属性,而不必添加自定义递增器

<?php
// Do not forget this part, required if you do not define a custom WP_Query
global $wp_query;

// Below you can find some useful properties in the WP_Query object
// Use them to their full potential

// Gets filled with the requested posts from the database.  
echo $wp_query->posts;

// The number of posts being displayed. 
echo $wp_query->post_count;

// The total number of posts found matching the current query parameters 
echo $wp_query->found_posts;

// The total number of pages. Is the result of $found_posts / $posts_per_page 
echo $wp_query->max_num_pages;

// Index of the post currently being displayed. (Can only be used within the loop)
while( have_posts() ) : the_post();
    echo $wp_query->current_post;
endwhile;

我相信这是一个好的、干净的解决方案

请在此处阅读有关WP_查询对象的更多信息:

我知道这个问题已经得到了回答,但我认为有更干净的方法可以做到这一点

您可以使用WP_查询对象中的几个有用属性,而不必添加自定义递增器

<?php
// Do not forget this part, required if you do not define a custom WP_Query
global $wp_query;

// Below you can find some useful properties in the WP_Query object
// Use them to their full potential

// Gets filled with the requested posts from the database.  
echo $wp_query->posts;

// The number of posts being displayed. 
echo $wp_query->post_count;

// The total number of posts found matching the current query parameters 
echo $wp_query->found_posts;

// The total number of pages. Is the result of $found_posts / $posts_per_page 
echo $wp_query->max_num_pages;

// Index of the post currently being displayed. (Can only be used within the loop)
while( have_posts() ) : the_post();
    echo $wp_query->current_post;
endwhile;

我相信这是一个好的、干净的解决方案

请在此处阅读有关WP_查询对象的更多信息:

可能重复的可能重复的可能重复的感谢,你有语法错误,第4行,其他明智的作品伟大。再次感谢,有吗?后留下的符号;只需要删除它是的,我纠正了这一点,两种解决方案都有效。你的更好,更简单。谢谢。谢谢,你有语法错误,第4行,其他明智的作品伟大。再次感谢,有吗?后留下的符号;只需要删除它是的,我纠正了这一点,两种解决方案都有效。你的更好,更简单。谢谢。这可能是一个真正的问题(我有时使用
$opened
布尔变量来检查循环后是否有任何标记被打开),但在这种情况下,OP的回声
,所以据我所知,没有机会出现未关闭的标记…你是对的,标记是关闭的-这就是我写这篇文章的原因“解决这个问题的需要…”我们不知道HTML(站点布局)“是否需要”clearfix。没有它,站点的外观可能会崩溃-或者根本不需要它。我想大多数时候clearfix是在一些浮点元素之后需要的,并且没有清理(“关闭浮点”)的情况下需要的下一个元素的行为可能不符合预期。这可能是一个真正的问题(我有时使用
$opened
布尔变量来检查循环后是否有任何标记被打开),但在这种情况下,OP的回声
,所以据我所知,没有机会出现未关闭的标记……你是对的,标记是关闭的——这就是我写这篇文章的原因。”解决这个问题的必要性…-我们不知道HTML(站点布局)“是否需要”最终的clearfix。如果没有它,站点的外观可能会崩溃-或者可能根本不需要它。我认为大多数时候clearfix是在一些浮点元素之后需要的,并且没有清除(“关闭浮点”)下一个元素的行为可能不符合预期。