wordpress怎么调用自定义分类下的子分类

新手求教
新建的自定义分类叫产品,products,模板taxonomy-products.php它的下面有子分类产品一,产品二
我在网上找的代码
1.functions.php加入

function get_category_root_id($cat)
{
$this_category = get_category($cat); // 取得当前分类
while($this_category->category_parent) // 若当前分类有上级分类时,循环
{
$this_category = get_category($this_category->category_parent); // 将当前分类设为上级分类(往上爬)
}
return $this_category->term_id; // 返回根分类的id号
}

2.模板里用

   <?php
    if(is_single()||is_category())
    {
    if(get_category_children(get_category_root_id(the_category_ID(false)))!="" )
    {
    echo wp_list_categories("child_of=".get_category_root_id(the_category_ID(false)). "&depth=0&hide_empty=0&title_li=&orderby=id&order=ASC");
    }
    }
    ?>

用了但是没有调用出来。是不是因为自定义分类的原因。
怎么在模板中调用这个产品的子分类产品一,产品二。产品模型都是一样的,用的模板也是一样的

我在官网找到一个方法,这种定死了term id。有没有好的方法不定term id

<?php
$term_id = 4;
$taxonomy_name = 'products';
$term_children = get_term_children( $term_id, $taxonomy_name );

foreach ( $term_children as $child ) {
    $term = get_term_by( 'id', $child, $taxonomy_name );
    echo '<li><a href="' . get_term_link( $child, $taxonomy_name ) . '">' . $term->name . '</a></li>';
}
?>
阅读 3.4k
1 个回答
用:get_the_term_list(get_the_ID(), 'products', '', ', ', '');

或者直接循环:

$taxonomy = 'portfolio_categories';
$terms = get_terms($taxonomy); // Get all terms of a taxonomy

if ( $terms && !is_wp_error( $terms ) ) {
    foreach ( $terms as $term ) {
        // 程序
    }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题