display only child category in your WordPress post loop og

Do you want your WordPress post loop to just show the child category?

Most WordPress themes will show all of a post’s categories, including parent and child categories, by default. What if you simply wanted to see the categories for children?

We’ll teach you how to easily display only the child category in your WordPress post loop in this article.

Advertisements

Showing only child categories inside WordPress post loop

When and why would you want to just show the child category?

To organize your content, WordPress comes with two basic taxonomies: categories and tags. Many websites use tags to organize specific subjects inside each post and categories to organize larger areas of the website.

There are also websites that employ categories to determine their website’s structure. A travel website, for example, might use categories to organize different sorts of places, while a culinary blog might use them to organize different types of cuisines.

To further organize your information, you may add child categories (or subcategories) for a parent category. For example, a travel website might submit an article to the Destinations » Europe category, where Europe is the child category.

By default, a WordPress theme will show all of a post’s parent and child categories.

All child and parent categories displayed

However, providing all of the categories for a post may not appear as organized and focused as displaying the most appropriate category. If that’s the case, you might wish to ignore the parent category and only show the kid category.

 

As a result, let’s look at how to just show the child category for a WordPress post.

 

Only displaying a WordPress post’s child category

Advertisements

This lesson assumes that you are familiar with copying and pasting custom code snippets in WordPress.

To begin, look for the code responsible for showing categories in your theme files. This is typically found in your theme’s single.php file.

You can replace the code responsible for displaying categories with the following code once you’ve found it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Get the IDs of child categories if any
$categories = get_the_category();
foreach( $categories as $category ) {
If ( $category->parent > 0 ) {
$child_cat_ID[] =  $category->term_id;
}
}
// If there are no child categories then display categories
If ( empty($child_cat_ID)) {
echo get_the_category_list( ' , ', '' );
// display child categories only
} else {
    
$child_cat_IDs = implode(', ', $child_cat_ID);
echo '<div class="post-categories">Filed under: ';
wp_list_categories( array(
        'separator' => ' ',
        'style'     => '',  
        'include' =>  $child_cat_IDs
    ) );
echo '</div>';
}

 

Remember to save your work and re-upload the theme files to your server.

You can now go to a single post with one or more child categories. You’ll see that the parent category is hidden and only the child categories are visible.

Only child categories displayed

There is only one flaw in this code.

The code will skip the standalone category if you have selected a parent category with child categories and another solo category. As a result, the “News” category would not appear in the example below.

Standalone category

If this is a problem for your design, you can substitute the following code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Get the IDs of categories
$categories = get_the_category();
foreach( $categories as $category ) {
If ( $category->parent > 0 ) {
$child_cat_ID[] =  $category->term_id;
//store child's parent category id to use later
$parent_cat_ID = $category->parent;
}
// find categories with no parent and no child
If ( $category->parent == 0 && $category->term_id !== $parent_cat_ID) {
$single_category[] = $category->term_id;
}
}
                
// Display part of the code
            
// if there are no child categories then go ahead and display all categories.
If ( empty($child_cat_ID)) {
echo get_the_category_list( ' , ', '' );
}
// If there are child categories then do this              
else {
$child_cat_IDs = implode(', ', $child_cat_ID) ;
$single_category_id = implode(', ', $single_category); 
// Combine child categories and categories with no children
$cats_to_include =   $child_cat_IDs . ', ' . $single_category_id ;
// Display them
echo '<div class="post-categories">Filed under: ';
wp_list_categories( array(
'separator' => ' ',
'style' => '',  
'include' =>  $cats_to_include
 ) );
echo '</div>';
}

Only parent categories will now be excluded by this code. Any solo categories, as well as child categories, will continue to be displayed.

Child categories and single standalone categories displayed

 

Advertisements

Leave a Reply