0
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
Labels:
Wordpress-Tutorials
Loading related posts...
Wordpress-Tutorials
8/30/2010 12:43:00 PM
Subscribe to:
Post Comments (Atom)