2013-04-09 28 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