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;
?>
Thank 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).
June 11 08 at 7:49 am
Thanks for the tip. Well explained & just what I was looking for!
July 4 08 at 8:05 pm
thanks , its working.
July 27 08 at 9:28 am
Any idea on how to get the category of the category page that you are on?
July 29 08 at 12:19 am
kj,
Not exactly sure but I think get_the_category or the_category will work.
July 29 08 at 8:18 am
Thank you, its great code.
August 6 08 at 3:06 am
thanks a lot, its working. great!
October 11 08 at 3:38 pm
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.
November 12 08 at 3:25 pm
How can I use this in a template to assign $id to the current category using this function? Thanks!
November 12 08 at 7:16 pm
Michael,
Like on category pages? This will work in the loop, not sure about outside of it.
<?php $id = $cat; echo $id; ?>November 12 08 at 11:41 pm
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!
November 13 08 at 7:38 am
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!
November 17 08 at 7:08 pm
Thanks for example.
In my case this is that what I’m find.
December 23 08 at 1:28 am
Thanks. I was looking for a function called get_category_ID() which obviously doesn’t exist.
Found this post via Google.
January 27 09 at 7:56 am
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!
February 11 09 at 4:31 pm
I’m getting an error: Call to undefined function get_cat_id() in /wpblog1/wp-functions.php on line 113
Anyway to fix that?
February 23 09 at 12:11 pm
try changing the file name from wp-functions.php to functions.php
February 23 09 at 12:20 pm
Thx man!
February 26 09 at 8:56 pm