Wordpress WP查询忽略特定类别的变量

Wordpress WP查询忽略特定类别的变量,wordpress,categories,Wordpress,Categories,好吧,我可能在这里遗漏了一些明显的东西,因为这是一件非常简单的事情,但由于某些原因,我无法让这个WP查询工作。我只想从用户当前使用get_the_category访问的分类页面查询帖子。像这样- $category = get_the_category(); $category_id = $category[0]->cat_ID; $category_items = new WP_Query( array(    'post_type' => 'post',    'cat'

好吧,我可能在这里遗漏了一些明显的东西,因为这是一件非常简单的事情,但由于某些原因,我无法让这个WP查询工作。我只想从用户当前使用get_the_category访问的分类页面查询帖子。像这样-

$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\u id为分类页面提供了正确的id,但在WP Query中引用它会使查询获得所有我的帖子,而不管类别如何。

尝试以下方法:

$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'
    )
);
试试这个:

$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'
    )
);

非常好,谢谢!为什么我不能使用这个ID呢?我不确定,但在我看来showposts=-1覆盖了cat参数。这很奇怪。哦,这个解决方案很好。再次感谢!非常好,谢谢!为什么我不能使用这个ID呢?我不确定,但在我看来showposts=-1覆盖了cat参数。这很奇怪。哦,这个解决方案很好。再次感谢!