php - 使用 get_posts() 返回随机帖子;在 Wordpress 中

标签 php wordpress

我目前正在尝试从 Wordpress 中的自定义帖子类型返回一篇随机帖子。目前,它返回的只是该自定义帖子类型中发布的最后一篇帖子。我目前正在使用此代码获取帖子:

<?php $posts = get_posts(array('posts_per_page' => 1, 'order_by' => 'rand', 'order' => 'ASC', 'post_type' => 'call_to_action')); if( $posts ): ?>
  <?php foreach ( $posts as $post ) : setup_postdata( $post ); ?>
    // Some code
  <?php endforeach; ?>
  <?php wp_reset_postdata(); ?>
<?php endif; ?>

最佳答案

'order_by' => 'rand' 更改为 'orderby' => 'rand'

关于php - 使用 get_posts() 返回随机帖子;在 Wordpress 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47263737/

相关文章:

php - 替换MySQL中的复杂字符串

css - 使用 css 仅选择表格中的特定元素

php - Woocommerce 允许选择一种变体

php - Wordpress 分页自定义帖子类型

php - 配置 PHP 以将错误存储在数据库中

php - 如何检查年度日期条件

php - json_decode 到数组或对象

php - 强大的 CMS 的骨架是什么

php - 准备好的 SQL 查询、UNION 和 LIMIT - 语法

javascript - 如何制作 Flickr 徽章打开新窗口