我希望使用以下代码随机获得当前类别中的5个帖子。
// Get all posts within current category, but exclude current post
$category_posts = new WP_Query( array(
'cat' => $categories[0]->term_id,
'post__not_in' => array( get_the_ID() ),
) );
您如何应用' 5帖子'限制和随机排序'到上面的代码?
答案 0 :(得分:0)
对我来说,我会使用get_posts(),但这些参数也适用于你的情况:
<?php $args = array(
'numberposts' => 5,
'category' => $categories[0]->term_id,
'orderby' => 'rand',
'exclude' => array( get_the_ID() ),
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args ); ?>
有关此内容的更多信息:https://codex.wordpress.org/Template_Tags/get_posts
答案 1 :(得分:0)
我使用了以下代码。
// Get all posts within current category, but exclude current post
$category_posts = new WP_Query( array(
'orderby' => 'rand',
'cat' => $categories[0]->term_id,
'post__not_in' => array( get_the_ID() ),
'posts_per_page' => 3,
) );