php - 如何在 WordPress 中显示 5 个随机帖子?我的循环不工作?
问题描述
在我添加查询以进一步自定义我的循环之前,循环运行良好。我究竟做错了什么?
我试过添加 wp_reset_postdata(); 结束前 while & end 如果无济于事。除非我没有看到另一个问题?
<?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()
需要知道要提取什么样的数据。
推荐阅读
- amazon-web-services - AWS s3 cp 无法从 gitlab 构建中找到文件?
- mysql - 使用 max 和 col 无法使用 Sequelize 映射
- python - 如何使用不和谐机器人创建新频道?
- excel - 计算未来值的功能 =)
- java - 登录限制尝试不适用于 servlet
- ruby-on-rails - 如何在 Rails 中重构嵌套的 as_json?
- ionic4 - 我想显示一首赞美诗的详细信息
- file - 如何获取文件的“BufReader”中的当前位置?
- javascript - 从 Vue.JS 中的 API 将项目 ID 传递给 Router-Link
- c++ - 使用链表从双端队列中删除最后一个节点时避免内存泄漏