在WordPress中获得当前类别内的5篇随机帖子
我只想得到5个职位在当前类别内随机使用以下代码在WordPress中获得当前类别内的5篇随机帖子,wordpress,Wordpress,我只想得到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(
// 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() ),
) );
// 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,
) );
如何将“5篇文章”限制和“随机排序”应用于上述代码?至于我,我会使用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 ); ?>
关于这方面的更多信息:我使用了以下代码
// 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() ),
) );
// 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,
) );
,