【问题标题】:Display custom post types of a certain category only仅显示特定类别的自定义帖子类型
【发布时间】:2017-09-17 00:34:22
【问题描述】:

我的网站上有一些faqs,我只想显示某个类别下的常见问题解答。类别包括workercompanytest

这是我的代码:

$faq = new WP_Query(array(
          'post_type'=>'faq',
          'order' => 'DESC',
          'category_name' => 'test',
        ));
  while($faq->have_posts()) : $faq->the_post();

这应该向我显示测试类别下的所有帖子,但它没有这样做。我也试过cat => (id),但仍然没有结果。循环总是空的。

有什么想法吗?

【问题讨论】:

标签: wordpress loops categories


【解决方案1】:

herehere 已经回答了这个问题,但我将总结适用于您的特定案例的答案。

category_name 参数需要是类别的 slug,而不是您正在使用的类别名称:

'category_name' => 'test'

您的某个类别有“测试”吗?

您可以使用以下其他参数:

cat (int) - use category id.
category_name (string)- use category slug (NOT name).
category__and (array) - use category id.
category__in (array) - use category id.
category__not_in (array) - use category id.

像这样检查你的结果:

$faq = new WP_Query(...);    
print_r($faq->posts); die();

这将在屏幕上显示帖子列表(用于调试目的)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-18
    • 1970-01-01
    • 2018-05-21
    • 1970-01-01
    • 1970-01-01
    • 2014-10-03
    • 1970-01-01
    相关资源
    最近更新 更多