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;
?>
42 Comments
Leave a Commenthey hey hey… First of all many many thanks for instantiating this useful topic. You won’t believe how much I was struggling to workout with categories, their titles, links etc.. for a custom WP project. I love it…
Best,
Linda
1st Sep 2010
Idem. Muchas Gracias!
8th Sep 2010
You made my life easier.
11th Sep 2010
TY TY TY!
goodness, why can;t the good chaps at WP just list the cat id on the category page, under posts?
sooo many plugins require it as an input and its so hard to find for a novice!
18th Sep 2010