【问题标题】:Wordpress - Getting posts in a custom taxonomyWordpress - 在自定义分类中获取帖子
【发布时间】:2012-02-17 02:29:07
【问题描述】:

我已经为 wordpress 问题苦苦挣扎了几个星期,但我就是想不通。

我创建了一个名为“cpt_used”的自定义帖子类型,在该自定义帖子类型中我创建了一个名为“tax_used”的自定义分类,它是一个类别列表

我需要做的是显示属于每个自定义分类的所有帖子,我就是想不通。

我目前的代码如下,每个类别中有多个帖子,但它没有显示任何内容

$args = array(
    'orderby' => 'name',
    'hide_empty' => 0,
    'taxonomy' => 'tax_used'
);
$categories = get_categories($args);

foreach( $categories as $category ) {

    $newargs = array(
        'category_name' => $category->slug,
        'taxonomy' => 'tax_used',
        'term' => 'cpt_used'
    );

    query_posts( $newargs );
    if (have_posts()) :
        while (have_posts()) : the_post();
            the_title();
        endwhile;
    endif;

}

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    $newargs 完全一团糟。试试这个:

    $newargs = array(
     'post_type' => 'cpt_used',
     'tax_query' => array(
      array(
       'taxonomy' => 'tax_used',
       'field' => 'slug',
       'terms' => $category->slug
      )
     )
    );
    

    记住 print_r() 有时会返回值以在开始迭代之前检查它是否正是您想要的;)

    【讨论】:

    • 这是一个相当古老的线程......但我有以下问题:我想测试是否有一个具有特定名称的帖子具有特定的分类名称。我尝试添加tax_query'post_type' => 'any' 和帖子名称。我知道帖子没有这个分类,但query_posts() 将其返回...
    猜你喜欢
    • 1970-01-01
    • 2016-10-28
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 1970-01-01
    • 2017-06-02
    • 2014-10-23
    • 2011-06-26
    相关资源
    最近更新 更多