我试图在某种循环中只输出第一个子级别(不是父级或孙子级)自定义分类法,因此我可以添加自定义字段/缩略图。
我创建了一个分层自定义的帖子类型'product-type'
它有几个级别的自定义分类法。
1级-零食
第2级-巧克力(在这个例子中只显示1)
第三关-牛奶巧克力,黑巧克力…等等。
在父分类法页面上,我已经能够用以下代码成功列出所有现有的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代码,这样我就可以输出(echo?)只有只有第一个子级别(不是父或孙子)分类法在某种循环,我可以插入div或自定义字段?
试试这个大小
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
}
?>