我需要将下面的子类别显示在wordpress侧栏中的相应父类别。
不使用插件我需要显示父类别,并按照以下方式在侧栏中设置子类别
Category1
--Sub1
--Sub2
Category2
--Cat2Sub1
--Cat2Sub2
答案 0 :(得分:1)
请尝试使用此代码
<?php
if(is_category()) {
$breakpoint = 0;
$thiscat = get_term( get_query_var('cat') , 'category' );
$subcategories = get_terms( 'category' , 'parent='.get_query_var('cat') );
if(empty($subcategories) && $thiscat->parent != 0) {
$subcategories = get_terms( 'category' , 'parent='.$thiscat->parent.'' );
}
$items='';
if(!empty($subcategories)) {
foreach($subcategories as $subcat) {
if($thiscat->term_id == $subcat->term_id) $current = ' current-cat'; else $current = '';
$items .= '
<li class="cat-item cat-item-'.$subcat->term_id.$current.'">
<a href="'.get_category_link( $subcat->term_id ).'" title="'.$subcat->description.'">'.$subcat->name.' ('.$subcat->count.' posts)</a>
</li>';
}
echo "<ul>$items</ul>";
}
unset($subcategories,$subcat,$thiscat,$items);
}
?>