Wordpress当前类别的子类别列表

时间:2014-02-04 21:19:16

标签: php wordpress categories

如何列出当前类别子类别列表。

我会尝试get_categories代码,但我不能。

像那样;

$args = array('child_of' => term_id );
$categories = get_categories( $args );
foreach($categories as $category) { 
    echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
    echo '<p> Description:'. $category->description . '</p>';
    echo '<p> Post Count: '. $category->count . '</p>'; 

2 个答案:

答案 0 :(得分:4)

将$ args替换为:

$current_cat = get_queried_object();

$args = array( 'child_of' => $current_cat->term_id, );

答案 1 :(得分:0)

function sub_category_list(){
  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.'</a>
            </li>';
        }
        echo "<ul>$items</ul>";
    }
    unset($subcategories,$subcat,$thiscat,$items);
}

}