List of Subcategories of the Current Wordpress Category

How can I list the list of subcategories of the current category.

I will try the get_categories code, but I cannot.

like this:

$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>'; 
+3
source share
2 answers

Replace $ args with:

$current_cat = get_queried_object();

$args = array( 'child_of' => $current_cat->term_id, );
+3
source
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);
}

}

0
source

All Articles