2016-09-17 148 views
-1

我想通过随机使用以下代码只在当前类别中获得5个帖子。在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()), 
     )); 

如何将'5职位'限制和'随机排序'应用于上述代码?

+0

https://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters,https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters – CBroe

回答

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

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, 
     )); 
相关问题