2016-09-17 147 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, 
     )); 
相關問題