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.

📣 Join the live AMA session with Adam Silverstein on open source and WordPress core! Register Now →

How To Get Categories and Subcategories in WordPress

Updated on December 8, 2021

2 Min Read
add categories and subcategories in wordpress

The the_category() template function is often used in WordPress theme development. It displays the parent category and any child categories. In several instances, this function could generate a long list. The problem becomes important when a website has a large number of categories and subcategories. In such WordPress get category cases, the_catagory() function often create a huge list that is often less than useful for the task.

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.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Owais Alam

is the WordPress Community Manager at Cloudways - A Managed WooCommerce Hosting Platform and a seasoned PHP developer. He loves to develop all sorts of websites on WordPress and is in love with WooCommerce in particular. You can email him at [email protected]

×

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Thankyou for Subscribing Us!

×

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

CYBER WEEK SAVINGS

  • 0

    Days

  • 0

    Hours

  • 0

    Mints

  • 0

    Sec

GET OFFER

For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now