This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

How To Get Categories and Subcategories in WordPress

Updated on September 28, 2025

3 Min Read
add categories and subcategories in wordpress

Key Takeaways

  • Retrieve specific post categories dynamically with wp_get_object_terms().
  • Fetch subcategories under parent categories for organized content display.
  • Access category name, description, and link using ID or slug.
  • Display hierarchical categories cleanly in templates for better readability.
  • Enhance site navigation and SEO by structuring categories effectively.

Managing categories and subcategories is essential for any WordPress site, especially as your content library grows. Efficiently retrieving categories helps organize posts, improves user navigation, and enhances SEO.

In 2025, developers still rely on WordPress core functions to fetch categories by ID, slug, or for specific posts. This guide provides practical code snippets to handle categories and subcategories with precision and flexibility.

Get Specific Post Category

The following code will get the category of a specific post. Please note that you have to add this code to either content-single.php or single.php

<?php  $taxonomy = 'category'; 
// ID Gets which assign post 
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
 
// Links seprator.
$separator_link = ', ';
 
if ( ! empty( $post_terms ) && ! is_wp_error( $post_terms ) ) {
 
    $term_ids = implode( ',' , $post_terms );
 
    $terms = wp_list_categories( array(
        'title_li' => '',
        'style'    => 'none',
        'echo'     => false,
        'taxonomy' => $taxonomy,
        'include'  => $term_ids
    ) );
 
    $terms = rtrim( trim( str_replace( '<br />',  $separator_link, $terms ) ), $separator_link );
 
    // show category post.
    echo  $terms;
}  ?>

Get Subcategory from Parent Category

First, get the subcategory from the parent category for a particular post. Next, add the following code in the post template where the index and archive post loop start. You could also add this code after the title.

<?php
$taxonomy = 'category';

// ID Gets which assign post 
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
$post_terms_specific = array (1);
$result_post=array_diff($post_terms, $post_terms_specific);

// Links seprator.
$separator = ', ';

if ( !empty( $result_post ) && !is_wp_error( $result_post ) ) {

 $term_ids = implode( ',' , $result_post);
 $terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $term_ids );
 $terms = rtrim( trim( str_replace( '', $separator, $terms ) ), $separator );

 // show category post.
 echo $terms;

}
?>

Name of Category Get by ID

<?php
//First Method
$id_name= '22';
echo get_the_category_by_ID($id_name);

//Second Method
echo get_cat_name(22);
?>

Description of Category (Through ID)

Category slug is the URL name of that category:

<?php

 $id_description = '22';
 echo category_description($id_description);

?>

Description of Category (Get by Slug)

Category slug is the URL name of that category:

<?php

 echo category_description( get_category_by_slug('uncategorized')->term_id ); 

?>

You can generate a link to a particular category using the id. Use the following code:

<?php
$id_link = '1';
$categorylink = get_category_link( $id_link );
echo esc_url($categorylink);
?>

Wrapping up!

In this article, I discussed how to get specific post category, subcategory from parent category, category name by ID, category description by ID and category link by ID.

If you need help with the code or would like to add to the discussion, do leave a comment below.

Frequently Asked Questions

Q. How do I fetch all categories for a post in WordPress?

Use wp_get_object_terms($post->ID, 'category') to retrieve all categories assigned to a post.

Q. Can I get subcategories dynamically for any parent category?

Yes, filter by 'parent' => $parent_id in get_categories() or get_terms() to fetch subcategories programmatically.

Q. How do I display category links instead of names?

Use get_category_link($id) and wrap it in an anchor tag to output clickable category links.

Q. Can I get a category description using its slug?

Yes, retrieve the term ID via get_category_by_slug('slug')->term_id and pass it to category_description().

Q. Are these functions compatible with WordPress 2025 versions?

Yes, all functions like get_categories(), wp_get_object_terms(), and wp_list_categories() remain fully supported and widely used in 2025.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Owais Khan

Owais works as a Marketing Manager at Cloudways (managed hosting platform) where he focuses on growth, demand generation, and strategic partnerships. With more than a decade of experience in digital marketing and B2B, Owais prefers to build systems that help teams achieve their full potential.

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour