Display WordPress Child Categories of Selected WordPress Parent Categories

Dealing with categories in WordPress is a repeating task for WordPress developers. It is sometime difficult for beginners to achieve the required result with these categories. I am writing this post to give you a quick guide on displaying WordPress child categories of a selected WordPress parent category in category page.

NOTE : this code should be used in category.php template. If you know what you are doing then it can also be used in other wordpress templates with minor modification.

namecheap affordale Hosting and domains

First take a look at the complete code (ready to copy and paste in your plugin/theme template).

if(is_category()){ // check if the current page is category page

$this_category = get_category($cat);
 $parent_cat_id =$this_category->cat_ID; // term id/category id of parent category
 $taxonomies = array(
 'taxonomy' => 'category' );

$args = array( 'child_of' => $parent_cat_id);

$childCategories = get_terms($taxonomies, $args);
 if (sizeof($childCategories)>0)

echo ' <div class="categories"> ';
 echo '<p> Sub Categories of '. get_cat_name( $parent_cat_id ) .'</p>';

foreach ( $childCategories as $child ) {

$child_link = sprintf( '<a href="%1$s" alt="%2$s">%3$s</a>', esc_url( get_category_link( $child ->term_id ) ),
 esc_attr( sprintf( 'View all posts in %s', 'textdomain' ), $child ->name ),
esc_html( $child ->name ));

echo sprintf( $child_link );

echo '</div><!-- categories div end-->';

}// end if

Code Explanation:

if(is_category) confirms that we are on category page (category.php).

get_category() function takes one required parameter which is category ID or category row object. This function gives us the ID of selected current category. With the help of this id we can check if it has any child categories.

get_category($cat) , $cat is a wordpress global variable which can be accessed only on category pages. This variable “$cat” contains all the data about selected category. We can get category id, slug and can also determine if this category is a parent category or not with the help of some additional checks.

get_terms($taxonomies,$args) , used to retrieve the WordPress child categories. $taxonomies and $args are two arrays of parameters, in $taxonomies array we require only categories thus have listed only category.

$taxonomies = array(
'taxonomy' => 'category' );

and in the $args array we are only interested in the child categories so we have entered only child_of parameter.

$args = array( 'child_of' => $parent_cat_id);

If you want to get your custom taxonomy you can replace the category with your taxonomy name like this,
$taxonomies=array ('taxonomy'=>'your_custom_taxonomy');.

Next we check if this category has any child categories.

if (sizeof($childCategories)>0)

get_cat_name($parent_cat_id) returns name of the category and accept id of the category as a parameter. Rest of the code above is pretty self explanatory..

if you have any problem/issue any where in the code do ask in comments and i will be glad to help.

  1. Hi dear thanks for this code snippet
    I copy paste this code in functions.php file of my selected theme in wordpress installation but it gives an error “Internal Server Error” What is the solution of this issue?

    • Hi Mohsin,
      I have updated the above code snippet, it was missing “}” at the end, now call this code in category.php template and it will work. If you want to use it in any other page template let me know.

  2. Does this code still work? I am trying to implement it in my site, but its not pulling the child categories, maybe I am doing something wrong. Latest version of wordpress etc

  3. Hi Dear,
    This Code Is Worked..
    Now Please Tell Me How To Create Pagination for This Code

Leave a reply