Are you looking for a way to display the current post’s category ID and/or category name? While there’s probably a plugin for this, we have created a quick code snippet that you can use to get post’s category name/ID in WordPress.
Instructions:
All you have to do is add this code to your theme’s post.php file or in a site-specific plugin:
<?php foreach((get_the_category()) as $category) { $postcat= $category->cat_ID; $catname =$category->cat_name; echo $postcat; echo $catname; } ?>
Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.
If you liked this code snippet, please consider checking out our other articles on the site like: 22 best WordPress themes for podcasting and how to create a popup form in WordPress.
WOW! I’ve searched so many articles and tried so many ‘solutions’ but this – THANK YOU for providing quick, straight to the point code that actually works. CANNOT thank you enough!