嗨,我想在 WordPress 中创建一个类别树,如下所示:
<ul>
<a href=""> PARENT1 </a>
<li><a href=""> CHILD 1-1</a></li>
<li><a href=""> CHILD 1-2</a></li>
.
.
.
</ul>
<ul>
<a href=""> PARENT2 </a>
<li><a href=""> CHILD 2-1</a></li>
<li><a href=""> CHILD 2-2</a></li>
.
.
.
</ul>
我想要以上述格式创建类别列表并仅显示有子项的类别并隐藏没有子项的类别的东西
我尝试过这样的事情,但它没有给我我想要的
<?php $args = array(
'type' => 'post',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'category',
'pad_counts' => false
);
$cats = get_categories( $args );
foreach( $cats as $cat) {
if($cat->parent == 0) {
$head = $cat->name;
$cat_id = $cat->term_id;
}
echo "<a href=''>" . $head . "</a>";
wp_list_cats("sort_column=NAME&optioncount=0&hierarchical=1&hide_empty=0&child_of={$cat_id}");
}
?>
(适用于任何分类法,包括“类别”)
$your_taxonomy='category';
function my_Categ_tree($TermName='', $termID, $separator='', $parent_shown=true ){
$args = 'hierarchical=1&taxonomy='.$TermName.'&hide_empty=0&orderby=id&parent=';
if ($parent_shown) {$term=get_term($termID , $TermName); $output=$separator.$term->name.'('.$term->term_id.')<br/>'; $parent_shown=false;}
$separator .= '-';
$terms = get_terms($TermName, $args . $termID);
if(count($terms)>0){
foreach ($terms as $term) {
//$selected = ($cat->term_id=="22") ? " selected": "";
//$output .= '<option value="'.$category->term_id.'" '.$selected .'>'.$separator.$category->cat_name.'</option>';
$output .= $separator.$term->name.'('.$term->term_id.')<br/>';
$output .= my_Categ_tree($TermName, $term->term_id, $separator, $parent_shown);
}
}
return $output;
}
然后就可以输出:
1)目标类别(分类)树,使用特定ID
echo my_Categ_tree($your_taxonomy, 0 );
2) 所有类别/分类法
foreach (get_terms($your_taxonomy, array('hide_empty'=>0, 'parent'=>0)) as $each) {
echo my_Categ_tree($each->taxonomy,$each->term_id);
}
感谢您的回复,我找到了解决方案: 这段代码运行良好
$args = array(
'type' => 'post',
'child_of' => 0,
'parent' => 0,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 0,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'category',
'pad_counts' => false
);
$cats = get_categories( $args );
foreach( $cats as $cat) {
if($cat->parent == 0) {
$parent_cat = null;
$head = $cat->name;
$head_id = $cat->term_id;
}
echo "<ul><a class='parent-category' href=''>" . $head . "</a>";
wp_list_cats("sort_column=NAME&optioncount=0&hierarchical=1&hide_empty=0&child_of={$head_id}&show_option_none=");
echo "</ul>";
}
代码:
<?php
$args = array(
'taxonomy' => 'product-type',
'hierarchical' => true,
'title_li' => '',
'hide_empty' => false
);
?>
用途:
<ul>
<?php wp_list_categories( $args ); ?>
</ul>
您也可以使用
wp_list_categories()
函数进行分类。
在这里阅读更多内容:
http://codex.wordpress.org/Template_Tags/wp_list_categories https://wordpress.stackexchange.com/questions/39125/custom-taxonomy-tree-view
这是一篇非常旧的帖子,但也许有一些更干净的代码。有关get_terms的所有选项,请参阅文档。请随意使其更通用:
function build_custom_category_tree ($activeCatId, $activeParentId, $parentId = 0) {
$output = '';
$terms = get_terms( array(
'taxonomy' => 'custom_categories',
'hide_empty' => true,
'hierarchical' => true,
'parent' => $parentId
) );
if (count($terms)) {
$output .= '<ul>';
foreach ($terms as $term) {
$output .= '<li class="custom-cat' . ($term->term_id === $activeParentId || $term->term_id === $activeCatId ? ' active' : '') . '">';
$output .= $term->name;
$output .= build_custom_category_tree($activeCatId, $activeParentId, $term->term_id);
$output .= '</li>';
}
$output .= '</ul>';
}
return $output;
}
然后在您的模板中:
<div>
<?= build_custom_category_tree($catIdOfPost, $rootIdOfCat) ?>
</div>