循环问题,我想要排除最后3


loop issue where I want to exclude last 3

嘿,我有这个wordpress设置,我不是一个真正的PHP开发人员。我想要实现的是循环排除最近创建的3个帖子。有办法做到这一点吗?

            <div class="grid">
                <?php
                // Start the loop.
                while ( have_posts() ) : the_post(); ?>
                    <div class="col-sm-6 col-lg-4">
                        <?php get_template_part( 'content', 'square' ); ?>
                    </div>
                <?php
                // End the loop.
                endwhile; ?>
            </div><!-- .grid -->
            <?php the_posts_pagination( array( 'mid_size' => 2, 'prev_text' => '<span class="glyphicon glyphicon-menu-left" aria-hidden="true"></span>', 'next_text' => '<span class="glyphicon glyphicon-menu-right" aria-hidden="true"></span>' ) );
            // If no content, include the "No posts found" template.
        else :
            get_template_part( 'content', 'none' );
        endif;
        ?>

定义一个变量来计算总帖子数($count = [total posts]),然后使用for循环遍历这些帖子,设置$count-3的最大迭代:

for($i=0; $i<($count-3); $i++){
    do something;
}

像这样的"标准"循环的post计数存储在全局$wp_query中。可以通过$GLOBALS['wp_query']->post_count访问。

<?php
// Get the post count and set a counter variable
$count = $GLOBALS['wp_query']->post_count; $i = 0;
while ( have_posts() && $i < $count - 3 ) : the_post(); ?>
    <!-- Post content loop -->
<?php $i++; endwhile; ?>