Wordpress - 在自定义分类中获取帖子

时间:2012-02-17 02:29:07

标签: wordpress

我已经在一个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;

}

1 个答案:

答案 0 :(得分:5)

$ newargs完全搞砸了。试试这个:

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

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