个性化阅读
专注于IT技术分析

如何不在同一页面上重复帖子?使用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>

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


#1


如果你有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>
赞(0)
未经允许不得转载:srcmini » 如何不在同一页面上重复帖子?使用WP_Query吗?

评论 抢沙发

评论前必须登录!