首页 > 解决方案 > 在每次页面刷新时获取不同的随机帖子

问题描述

我需要帮助在每次页面刷新时显示不同的随机帖子。

我正在使用函数 orderby=rand,但这会在我拥有的每篇博文上给出相同的随机帖子。我需要在每次页面刷新时都有不同的帖子,不知道我该如何实现。

我目前有这个代码:

<div class="container">

    <div class="ltblg text-center">Related Posts</div>
    <div class="row">
        <?php
            $args = array( 'posts_per_page' => '3','order'=>'ASC','orderby'=>'rand' );
            $recent_posts = new WP_Query($args);
            while( $recent_posts->have_posts() ) :
            $recent_posts->the_post()
        ?>

        <div class="col-md-4 ">
            <div class="blgbox">

                <div class="latebog">
                    <?php
                        if (has_post_thumbnail()) {
                            the_post_thumbnail();
                        } else {
                               echo "<img src='/wp-content/uploads/2018/04/bydeaulfblog.png'/>";
                        }

                    ?>
                </div>

                <div class="blogheading blglist relttle">
                    <a href="<?php echo get_permalink(); ?>">
                        <?php echo wp_trim_words( get_the_title(), 15 ); ?>
                    </a> 
                </div>

                <div class="blgdate text-uppercase">
                    <?php the_time('F jS, Y') ?>
                </div>

                <div class="blgtet">
                    <p>
                        <?php echo wp_trim_words( get_the_content(), 25, '...' );?>
                    </p>
                </div>

                <a href="<?php echo get_permalink(); ?>" class="blgrdbtn">Read More
                    <span><img src="/wp-content/uploads/2018/03/rightarrw.png" /></span>
                </a>
            </div>
        </div>
        <?php endwhile; ?>
        <?php wp_reset_postdata();
              # reset post data so that other queries/loops work 
        ?>
    </div>
</div>

提前致谢!

标签: phpwordpressposts

解决方案


推荐阅读