如何从自定义 post 类型的父类别中获取子类别并显示在下拉列表中?

How to get child categories from parent category of custom post type and display in dropdown?

最后,我可以从自定义 post 类型的类别中获取子类别,并且一切正常。问题是当我想要获得相同但带有响应下拉菜单时。有可能吗?我也需要隐藏空术语。谢谢!

这里是代码:

<ul>
<?php $tax = get_term_by('slug', 'cursos', 'portfolio_category');
$tax_id = $tax->term_id;
$args = array(
'child_of'   => $tax_id,
'taxonomy' => 'portfolio_category',
'orderby' => 'name',
'show_count' => 0,
'pad_counts' => 0,
'hierarchical' => 0,     
'title_li' => '',
'hide_title_if_empty' => 0
);
wp_list_categories($args); ?></ul>

您可以使用 get_categories 获得 portfolio_category。试试下面的代码。

<ul class="show_on_desktop">
    <?php 
        $tax    = get_term_by('slug', 'cursos', 'portfolio_category');
        $tax_id = $tax->term_id;

        $args = array(
            'child_of'     => $tax_id,
            'taxonomy'     => 'portfolio_category',
            'orderby'      => 'name',
            'show_count'   => 0,
            'pad_counts'   => 0,
            'hierarchical' => 0,     
            'title_li'     => '',
            'hide_empty'   => true
        );
        wp_list_categories($args); 
    ?>
</ul>

<?php 
    $cats = get_categories( array( 
        'child_of'   => $tax_id,
        'taxonomy'   => 'portfolio_category',
        'hide_empty' => true
    ) ); 
?>
<select class="show_on_mobile">
    <?php foreach ( $cats as $cat ) : ?>
        <option value="<?php echo get_term_link( $cat->term_id ); ?>"><?php echo $cat->name; ?></option>
    <?php endforeach; ?>
</select>