Chat with us, powered by LiveChat

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

May 17, 2017

2 Min Read
add categories and subcategories in wordpress
Reading Time: 2 minutes

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

Start Growing with Cloudways Today!

We never compromise on performance, security, and support.

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 owais.alam@cloudways.com

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

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!