Get category ID using category name

0

default_thumb When coding a WordPress site, you often need to get a category ID. If you ever wanted to be able to get a category ID from the category name, then just read this recipe, I’m pretty sure you will like it.

As usual, let's start by pasting the function in your functions.php file:

function get_category_id($cat_name){
$term = get_term_by('name', $cat_name, 'category');
return $term->term_id;
}


Once you saved the file, just call the function with your category name as a parameter. Example:



$category_ID = get_category_id('WordPress Tutorials');


You could also use



$category_ID = get_cat_id(‘WordPress Tutorials’);


Works the same and doesn’t require a custom function in functions.php. Here’s the function reference : http://codex.wordpress.org/Function_Reference/get_cat_ID

Loading related posts...
Reactions: 

0 comments:

Post a Comment

2010 WEBSITE20. All rights reserved.