获取自定义的后分类值

问题描述 投票:0回答:1

我试图通过分类名称和帖子类型来调用分类法值

$res=get_categories('taxonomy=category&type=course_type'); 

我正确地得到了结果。但是当我传递第二个自定义分类名称时,它给了我空白数组。就像我的第二个分类学是研究,我正在传递它

$res=get_categories('taxonomy=study&type=course_type'); 

它不适合学习。

我已经创建了研究分类法

register_taxonomy(  'study', 'course_type',  
array(
    'hierarchical' => false,
    'label' => 'Mode Of Study', 
    'query_var' => true, 
    'rewrite' => array( 'slug' => 'study'))
    );

这段代码并插入一些学习价值,但我无法在我的页面上得到它。任何人都可以帮助我得到它。谢谢

wordpress custom-post-type
1个回答
0
投票

使用此代码。

$custom_terms = get_terms('study');

foreach($custom_terms as $custom_term) {
    wp_reset_query();
    $args = array('post_type' => 'course_type',
        'tax_query' => array(
            array(
                'taxonomy' => 'study',
                'field' => 'slug',
                'terms' => $custom_term->slug,
            ),
        ),
     );

     $loop = new WP_Query($args);
     if($loop->have_posts()) {
        echo '<h2>'.$custom_term->name.'</h2>';

        
     }
}
© www.soinside.com 2019 - 2024. All rights reserved.