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

Big*_*ies 1 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;

}
Run Code Online (Sandbox Code Playgroud)

小智 5

$ newargs完全混乱了。尝试这个:

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

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