Today I have a quick tip for all the WordPress theme developers. I’m actually working on a theme right now and thought I would share a tip that will make your themes much more user friendly.
Often when developing themes for one reason or another you want the end user to give you the ID for a category. This could be so you could call certain categories, or exclude them, or create custom menus etc. In previous WordPress versions the category ID’s were listed along side the categories so it wasn’t as hard for people to find the ID’s. This isn’t true anymore, and for less savvy template users, finding a category ID may not be so easy. At first I simply decided to give users a chart in the theme options page(you can see this in action in my CheckMate theme), and that works OK. But it isn’t ideal.
No Worries, We Got A Function
That function is get_cat_id() and can make end users life easier. You can use this function to take the category name get it’s ID number to use in functions like query_posts. Now all your users have to do is enter the category name in the theme options panel(or name a category something specific like “video”).
Examples
This is a very basic example of how to use the function.
<?php
$id = get_cat_id('video');
$q = "cat=" . $id;
query_posts($q);
if (have_posts()) : while (have_posts()) : the_post();
the_content();
endwhile; endif;
?>
39 Comments
Leave a CommentThank you for this information – just what I needed to modify a page template I’m working on (want to include trailers to some posts in certain categories on a page using get_posts(), without messing up existing retrieved post).
11th Jun 2008
Thanks for the tip. Well explained & just what I was looking for!
4th Jul 2008
thanks , its working.
27th Jul 2008
Any idea on how to get the category of the category page that you are on?
29th Jul 2008
single_cat_title();16th Nov 2009
Sorry, try this:
$cat = get_query_var(‘cat’);
7th Feb 2010
kj,
Not exactly sure but I think get_the_category or the_category will work.
29th Jul 2008
Thank you, its great code.
6th Aug 2008
thanks a lot, its working. great!
11th Oct 2008
Thank you! All I needed was a category ID and WP doesn’t list it anywhere in the admin panel for some reason…but I got it with the get_cat_id function.
12th Nov 2008
How can I use this in a template to assign $id to the current category using this function? Thanks!
12th Nov 2008
Michael,
Like on category pages? This will work in the loop, not sure about outside of it.
<?php $id = $cat; echo $id; ?>12th Nov 2008
See this thread to the solution for what I was trying to do in relation to my question:
http://wordpress.org/support/topic/217104?replies=4
Thanks for your reply!
13th Nov 2008
yeah! the puzzle piece I was missing!
now I can do:
<a href=”">
to display the categories list from an option I set on the themes options, which of course, in order to be user friendly had to be with names.
Thanks!
17th Nov 2008
Thanks for example.
In my case this is that what I’m find.
23rd Dec 2008
Thanks. I was looking for a function called get_category_ID() which obviously doesn’t exist.
Found this post via Google.
27th Jan 2009
Thanks for the advice. When you allow SEO Permalinks and All In One SEO for Internet marketing purposes, you can’t find the ID’s for love nor money! Now I can – cheers dude!
11th Feb 2009
I’m getting an error: Call to undefined function get_cat_id() in /wpblog1/wp-functions.php on line 113
Anyway to fix that?
23rd Feb 2009
try changing the file name from wp-functions.php to functions.php
23rd Feb 2009
Thx man!
26th Feb 2009
Awesome, Exactly what i needed. Thnx for sharing :)
19th Aug 2009
ok, I want to apply your concept to this code:
this code generates a link to the blog ID dynamically. So if I go to admin and change my permalinks or whatever, it updates it automatically.
How do I do the same thing, but to categories? Do you have any clue? your time and effort is much appreciated! :)
24th Oct 2009
Try posting the code without should work.
24th Oct 2009
Just what I needed. Thanks for sharing (btw, your site rocks! )
3rd Nov 2009
I ran into the same problem as someone further up.. How to get the category ID from the category page.
Use get_query_var(‘cat’)
Thanks for a great tip.
29th Nov 2009
Thanks for that, worked like a charm!
6th Feb 2010
Baller!
This wasn’t exactly what I was looking for but it led me to try get_cat_name() and for this, I thank you!
-Josh
3rd Dec 2009
Hi Curtis – if you’re still tending to comments on this page well over a year after posting ;) I’ve got a question.
Any idea how to apply this in a scenario where you want to EXCLUDE the category in question?
Basically, just like the basic example of how to use the function that you gave in the post – difference being in the ‘query_posts();’ tag, how do you say “exclude $q”? Where the variable $q is the cat ID you’ve just found using the get_cat_id function… make sense?
11th Dec 2009
David,
you just need to change line 3 from
$q = "cat=" . $id;
and add a negative sign infront of the category id like
$q = "cat=-" . $id;
that should work.
or just skip the $q part and do it like this:
query_posts("cat=-" . $id);
11th Dec 2009
Thank you for sharing this code.
24th Jan 2010
I tried lots of different things to echo the category ID on a category archive page. In the end this simply code did the trick….
Whoda thunk it was so easy.
21st Apr 2010
I have permalinks set up.
Before permalinks setup I have query string like this,
http://localhost/wordpress/products-page?category=2&product_id=11
And by changing it to permalinks to numeric,
It shows ;
http://localhost/wordpress/products-page/po-campo1/product-3
So, how can I get category_id and product_id at the top of the page?
If you know that please share it with me, I really need it.
Thanks.
22nd Apr 2010
If you just want to print out the category ID in the template you can do this:
$id = get_cat_id('po-campoi');
echo $id;
echo $post->ID;
The $post->ID code may not work for the product ID since I’m not sure how that’s setup. You could try:
print_r($post);to see the entire array to find the right key for product_id.
22nd Apr 2010
How can i find category ID of curent post?
I want to make custom background of post page depending of category.
thx
2nd May 2010
$categories = get_the_category();2nd May 2010
thx buddy
in fact i need term_id:
$categories=get_the_category();
$tmp=$categories[0];
$cat_id = $tmp->term_id;
$cat_id can be used (with get_bloginfo(‘template_url’)) for custom style
thx again
2nd May 2010
Just to add, you required the category ID then you can also view it by turning off permalinks, refreshing your wp site, noting it down, and then turning permalinks back on. Thought Id mention this just incase anyone was unsure!
6th May 2010
And if anyone is wanting to do the opposite..
get_cat_name(’2′);
18th Jul 2010