2013-04-09 56 views
1

好吧,我可能在這裏丟失了一些明顯的東西,因爲這是一件非常簡單的事情,但由於某種原因,我無法使WP Query工作。我只想從用戶當前正在訪問的類別頁面查詢帖子(使用get_the_category)。像這樣 -WP查詢忽略特定類別的變量

$category = get_the_category(); 
$category_id = $category[0]->cat_ID; 

$category_items = new WP_Query(array(
   'post_type' => 'post', 
   'cat' => $category_id, 
   'showposts' => -1, 
   'orderby' => 'rand' 
   ) 
); 

$ CATEGORY_ID讓我對分類頁面正確的ID,而是指它WP查詢裏面,使查詢得到我的所有帖子,無論類別。

回答

1

試試這個:

$category = get_the_category(); 
$category_name = $category[0]->cat_name; 

$category_items = new WP_Query(array(
    'post_type' => 'post', 
    'category_name' => $category_name, 
    'showposts' => -1, 
    'orderby' => 'rand' 
    ) 
); 
+0

這工作出色,謝謝!我爲什麼不能使用這個ID呢? – 2013-04-09 08:37:45

+0

我不確定,但在我看來,showposts = -1覆蓋了cat參數。 – user850010 2013-04-09 08:40:10

+0

這很奇怪。哦,這個解決方案將會很好。再次,謝謝! – 2013-04-09 08:45:27