繁体   English   中英

如何不在同一页面上重复帖子? 使用WP_Query吗?

[英]How to not repeat posts on the same page? With WP_Query?

我需要在主页上显示所有会有所不同的帖子。 也就是说,它们不会在同一页面上重复。 我使用WP_Query显示它们,但是对于帖子的每一列,我都需要有另一个“ posts_per_page”。 怎么做?

我尝试这样做:

<?php $my_query = new WP_Query($args); ?>

this <?php  $my_query->set('posts_per_page', 3);
$my_query->query($my_query->query_vars); ?>

这是我的代码示例一:

<div class="row">
                <div class="col-xl-9 col-lg-9 col-md-12 featured-posts">
                    <?php  $my_query->set('posts_per_page', 3); $my_query->query($my_query->query_vars); ?>
                    <?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
                        <article class="post post-id-<?php echo the_ID(); ?>">
                            <a class="post-thumbnail" href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a>
                            <div class="post-content">
                                <h3 class="post-title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
                                <?php healthybroom_posted_on(); ?>
                                <?php healthybroom_entry_footer(); ?>
                                <p class="post-excerpt"><?php echo get_the_excerpt(); ?></p> 
                            </div>
                        </article>
                    <?php endwhile; ?>
                </div>
                <div class="col-xl-3 col-lg-3 col-md-12 front-random-posts">
                    <?php  $my_query->set('posts_per_page', 5); $my_query->query($my_query->query_vars); ?>
                    <?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
                        <article class="post post-id-<?php echo the_ID(); ?>">
                            <a class="post-thumbnail" href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a>
                            <div class="post-content">
                                <?php healthybroom_entry_footer(); ?>
                                <h3 class="post-title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
                                <?php healthybroom_posted_on(); ?>
                            </div>
                        </article>
                    <?php endwhile; ?>
                </div>
            </div>

我希望不要简单重复这些帖子。

如果您有2个带有随机帖子的循环,并且希望避免在2个循环中有相同的帖子,则可以使用以下代码:

<!-- We create an array for saving the IDs of the post displaying in the first loop -->
<?php $store_posts_ids = array(); ?>

<div class="row">
    <div class="col-xl-9 col-lg-9 col-md-12 featured-posts">
        <?php 
            $my_query->set('posts_per_page', 3);      
            $my_query->query($my_query->query_vars); 
        ?>
        <?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>

             <!-- We store the post id for the second loop -->
             <?php array_push( $store_posts_ids, get_the_ID() ); ?>

             <!-- Your code here -->

        <?php endwhile; ?>
    </div>

    <div class="col-xl-3 col-lg-3 col-md-12 front-random-posts">
        <?php 
            /*
            If you want to display 5 posts, you need to have a loop
            of 7 posts because you have 3 posts in your first loop
            */
            $number_post = 0;
            $my_query->set('posts_per_page', 5);      
            $my_query->query($my_query->query_vars); 
        ?>
        <?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
        <?php array( $store_posts_ids, get_the_ID() ); ?>

             <!-- If we have our 5 posts, no need to display more posts -->
             <?php if( $number_post < 5 ): ?> 

                 <!-- If the post is not in the first loop -->
                 <?php if( !in_array( $store_posts_ids, get_the_ID() ) ): ?>
                     <?php $number_post = $number_post + 1; ?>   

                     <!-- Your code here -->

                 <?php endif; ?>

             <?php endif; ?>

        <?php endwhile; ?>
     </div>
</div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM