orthopedic pain management

Flashuser

  • Pin It

How to find a WordPress category ID

Did you wonder how you can find a category ID for a post to use it with WordPress template tags or build a custom function with it? Nothing simple. Here’s an easy tip to find any category ID. This technique applies also for WordPress tags or custom taxonomies terms that you might be using in your website.

Find WordPress Category ID

This value can be found conveniently in the WordPress admin area.

a. Login to the WordPress admin panel and navigate to Posts -> Categories section.
b. Rollover the mouse for a specific category found in the right panel and will see in the browser’s status bar the corresponding url with the category ID ( e.g: tag_ID = 6 – you need the number )

wordpress-category-id

c. If you want to find the first category of a post dynamically, you can insert this piece of code within the loop.

<?php $category = get_the_category(); $firstCategID = $category[0]->cat_ID; ?>

d. For custom taxonomies the dynamic approach is a little bit different and you have to use get_the_terms function. Here’s a piece of code for doing that:

<?php
$terms = get_the_terms( get_the_ID(), 'section' );
foreach ($terms as $term) { echo $term->term_id; }
?>

where section is the name for the custom taxonomy.

I hope you enjoyed this article, happy coding!

Author: Alin

Passionate about web and graphic design, photography and climbing. Follow me at Twitter , Facebook and Google+