All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

WordPress Get Post Category Name In Loop

Last Updated : Mar 11, 2024

WordPress Get Post Category Name In Loop

In this article we will show you the solution of WordPress get post category name in loop, WordPress users commonly refer to the code which displays blog articles on the WordPress theme as the WordPress Loop, while WordPress Codex refers to it as The Loop.

The Loop is thought to be the key component of a WordPress theme. Based on the parameters, it is in charge of showing blog articles on the current page.

A loop, or WordPress loop, is a section of PHP code that shows posts from WordPress. The posts just on current web pages are displayed by WordPress themes using a loop.

The loop is built around a few functions that are used to display new posts as they are executed.

Among the most crucial parts of the WordPress code is the WordPress Loop, which contains a number of Template Tags for publishing, formatting, and organizing post data.

There are numerous tags that can be used to check on conditions, but thankfully, WordPress has made these tags largely self-explanatory.

Like "is home," which determines if the current view is the main site. We can start certain Loops using Conditional Tags.

Step By Step Guide On WordPress Get Post Category Name In Loop :-

// 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>';
}

Code 2

// 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>';
}
  1. You must first locate the code within your theme files that displays categories. This is typically found inside your theme's single.php file.WordPress Get Post Category Name In Loop
  2. For additional information, visit our cheat sheet on the WordPress template hierarchy, which explains which template files are utilized to display certain parts of a WordPress theme.
  3. You can substitute the code for the code that displays categories after you've found it.
  4. Don't forget to upload all theme files back onto your server after saving your changes.
  5. The single post that includes one or even more child categories can now be seen. You'll see that it will only show the kid categories and hide the parent category.
  6. This code has one flaw, though.WordPress Get Post Category Name In Loop
  7. The code will exclude the standalone category if you choose a parent category having child categories and another solo category. As a result, the "News" category wouldn't be visible in the example below.
  8. Just use code instead if this is a problem for your design requirements.
  9. Only parent categories will now be excluded by this code. Any solo categories and their offspring will still be displayed.

Conclusion :-

The majority of WordPress themes would automatically display both the parent and the child categories for a post's categories.

What if only you wanted the kid category to be visible? To organize your content, WordPress utilizes two basic taxonomies named categories and tags.

There are additional websites that employ categories to specify their website's organizational structure.

For example, a travel website might utilize categories for various locations, while a culinary blog might use them for various cuisines.

I hope this article on WordPress get post category name in loop helps you and the steps and method mentioned above are easy to follow and implement.