【问题标题】:In WordPress loop, how do I check how many post have been looped through?在 WordPress 循环中,我如何检查已循环通过的帖子数量?
【发布时间】:2017-04-07 03:30:54
【问题描述】:

我的目标是在每四篇文章之后打印一些额外的 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. ?>

【问题讨论】:

标签: wordpress loops


【解决方案1】:

我知道这个问题已经得到解答,但我认为有更简洁的方法可以做到这一点。

您可以在 WP_Query 对象中使用几个有用的属性,而不必添加自定义增量器。

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

使用 $current_post 属性时,您的循环将如下所示。

<?php global $wp_query; ?>
<?php while ( have_posts() ) : the_post(); ?>
    <?php wc_get_template_part( 'content', 'product' ); ?>
    <?php
        if ( ( $wp_query->current_post + 1 ) % 4 === 0 ) {
            echo '<div class="clearfix"></div>';
        }
    ?>
<?php endwhile; // end of the loop. ?>

我相信这是一个很好且干净的解决方案。

在此处阅读有关 WP_Query 对象的更多信息: https://codex.wordpress.org/Class_Reference/WP_Query

【讨论】:

    【解决方案2】:

    在我之前的答案中可能存在一个错误:它会在每四个帖子的末尾回显“结束”标签 - 但是 如果帖子数是 5 怎么办?(或任何其他数字不能被 4 整除?当然,解决这个问题的需要可能因 HTML 不同而异。)

    用5看这个例子:

    --发布 1--
    --发布 2--
    --发布 3--
    --post 4--
    --结束标签--
    --post 5--
    ??????

    您可以看到,在第 5 次发布之后,循环没有用结束标记关闭。

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

    <?php
        // taking @Jordi Nebot's code from his answer - thank you
        if ( have_posts() ) : $count = 1;
    ?>
        <?php while ( have_posts() ) : the_post();?>    
        <!-- do stuff ... -->
        <?php
            // the conditions in the if are triggered after every 4th post
            // OR if all the posts have been echoed OR all the posts for 
            // this page have been echoed
            if ( $count % 4 == 0 || $wp_query->found_posts == $count || $wp_query->posts_per_page == $count ):
        ?>
            <!-- extra stuff every four posts -->
        <?php endif; ?>
        <?php $count++; endwhile; ?>
    <?php endif; ?>
    

    【讨论】:

    • 这可能是一个真正的问题(我有时使用$opened 布尔变量来检查循环后是否打开了任何标签)但在这种情况下,OP 会回显&lt;div class="clearfix"&gt;&lt;/div&gt;,所以没有机会据我所知,对于未封闭的标签...
    • 你是对的,标签是封闭的——这就是为什么我写了“解决这个问题的必要性……”——我们不知道,HTML(网站布局)是否需要“最后的那个clearfix。没有它,网站的外观可能会分崩离析——或者根本不需要它。我认为大多数时候在一些浮动元素之后需要 clearfix - 如果不清理(“关闭浮动”),下一个元素可能不会按预期运行。
    【解决方案3】:

    试试这个

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

    【讨论】:

    • 谢谢,您有语法错误,第 4 行,其他方面效果很好。再次感谢。
    • 有吗? ; 后面的符号只需要删除它
    • 是的,我纠正了这一点,两种解决方案都有效。你的更好更简单。谢谢。
    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多