我目前正在尝试从 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/