繁体   English   中英

如何在WordPress中显示5条随机帖子? 我的循环无法正常工作吗?

[英]How to display 5 random posts in WordPress? My Loop isn't working?

在添加查询以进一步自定义循环之前,该循环运行良好。 我究竟做错了什么?

我尝试添加wp_reset_postdata(); 收盘结束前,而&结束则无济于事。 除非我没有其他问题?

    <?php
    $args = array(
        'orderby'        => 'rand',
        'posts_per_page' => '5',
    );
    $my_query = new WP_Query( $args );
    if ( $my_query->have_posts() ) {
        while ( $my_query->have_posts() ) {
            $my_query->the_post();

            get_template_part( 'template-parts/content/entry_cards', get_post_type() );

        } // end while
    } // end if

    ?>

我希望能够以随机顺序查看5个WordPress帖子(如数组中所述)。

我相信:

$args = array(
    'orderby'        => 'rand',
    'posts_per_page' => '5',
);

应该:

$args = array(
    'post_type'      => 'post',
    'orderby'        => 'rand',
    'posts_per_page' => '5',
);

WP_Query()需要知道要提取哪种数据。

暂无
暂无

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

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