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.

CloudwaysCDN — a powerful solution that offers superior performance and satisfied global audience for your business. Read More

Get Categories and Subcategories In WordPress

Updated on  June 28, 2018

2 Min Read
wordppress categories and subcategories
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 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

Get Subcategory From Parent Category

First get the subcategory from the parent category for 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.

Name Of Category Get By Id

Description Of Category (Through ID)

Category slug is the URL name of that category:

Description Of Category (Get By Slug)

Category slug is the URL name of that category:

Category Link (Get By ID)

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

Conclusion

In this article, I discussed how to get specific post category, sub category 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

Make Your WordPress Website 100% Faster.

Host it now on Cloudways WordPress Hosting Platform.

About The Author

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

Stay Connected:

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

THERE’S MORE TO READ.