首页 文章

输出(echo?)将父自定义分类的子节点引导到循环中

提问于
浏览
0

我试图在某种循环中仅输出第一个子级别(不是父级或孙级)自定义分类法,因此我可以为其添加自定义字段/缩略图 .

我创建了一个名为“product-type”的分层自定义帖子类型

并且有一些级别的自定义分类法 .

1级 - 小吃

2级 - 巧克力(本例仅显示1)

3级 - 牛奶巧克力,黑巧克力......等等 .

在父分类页面上,我已经能够使用以下代码成功列出所有现有的2级分类:

<?php 
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); 
if ($term->parent == 0) {  
wp_list_categories('taxonomy=product-type&depth=1&show_count=0
&title_li=&child_of=' . $term->term_id);
} else {
wp_list_categories('taxonomy=product-type&show_count=0
&title_li=&child_of=' . $term->parent); 
}
?>

输出看起来像这样:

<li class="cat-item cat-item-7">
<a href="..." title="...">Chocolates</a> (14)
</li>

我的问题是,我如何编辑上面的PHP代码,以便我可以输出(回声?)只有第一个孩子级别(不是父或孙子)分类在某种循环,我可以插入div或自定义字段?

1 回答

  • 1

    尝试这个尺寸 .

    https://codex.wordpress.org/Function_Reference/get_terms

    <?php 
    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); 
    if ($term->parent == 0) {  
        $terms = get_terms( 'product-type', 'child_of='.$term->term_id );
    } else {
        $terms = get_terms( 'product-type', 'child_of='.$term->parent );
    }
    foreach($terms as $term) {
        // Your custom HTML
    }
    ?>
    

相关问题