[英]How to display 5 random posts in WordPress? My Loop isn't working?
The loop was working fine until I added the query to further customize my loop. 在添加查询以进一步自定义循环之前,该循环运行良好。 What am I doing wrong? 我究竟做错了什么?
I've tried adding wp_reset_postdata(); 我尝试添加wp_reset_postdata(); before the closing end while & end if to no avail. 收盘结束前,而&结束则无济于事。 Unless there's another issue that I'm not seeing? 除非我没有其他问题?
<?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
?>
I would like to be able to see 5 of my WordPress posts in a random order (as stated in my array). 我希望能够以随机顺序查看5个WordPress帖子(如数组中所述)。
I believe this: 我相信:
$args = array(
'orderby' => 'rand',
'posts_per_page' => '5',
);
Should be: 应该:
$args = array(
'post_type' => 'post',
'orderby' => 'rand',
'posts_per_page' => '5',
);
WP_Query()
needs to know what kind of data to pull. WP_Query()
需要知道要提取哪种数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.