I’m trying to create a custom category template that will display a list of the current category’s child categories. Besides the name and link to the category I also want to add a thumbnail and the category description.
I tried the following code but it didn’t return anything:
<?php
$catid = get_category(get_query_var( 'cat' ));
$termchildren = get_term_children( $catid, 'category' );
echo '<ul>';
foreach( $termchildren as $cat ) {
$term = get_term_by( 'id', '$cat', 'category' );
echo '<li>'.$term->name.'</li>';
}
?>
I’ve left out the other parts of the html list at this stage as I’m just trying to get it to work and will add them in later.
1 Answer
This piece of code will return you the child categories of a parent-
$cat = get_category( get_query_var( 'cat' ) );
$cat_id = $cat->cat_ID;
$child_categories=get_categories(
array( 'parent' => $cat_id )
);
Just pass the category id to $cat_id
variable which children you want. After that you can design or print those as you want. Example-
foreach ( $child_categories as $child ) {
// Here I'm showing as a list...
echo '<li>'.$child ->cat_name.'</li>';
}
Hope this is gonna help.