How to : Get Category ID in WordPress

Posted by cikul | Posted in PHP, wordpress | Posted on 23-03-2009-05-2008

3

If you’re working on wordpress Theme, sometimes you need to know the categories ID for some categories. For example, in my cases, i need to exclude  “projects” category from my blog posts lists, you can use query_posts(‘cat=-3) for example if you know that project assigned with category ID=3. But if your themes using by several sites, you will difficult to define the category ID, because it will be different on another wordpress installation.

You can use function get_cat_id(), You can use this function to take the category name get it’s ID number to use in functions like query_posts
above.

Example :

<?php
$id = get_cat_id('projects');
$q = "cat=" . $id;
query_posts('cat=-'.$q);
if (have_posts()) : while (have_posts()) : the_post();
the_content();
endwhile; endif;
?>

Comments posted (3)

mantap kang salam kenal saja
ini yang dicari byme
byme

I haven’t a clue how to use WordPress and Comicpress to create sites and publish comic strips! Please help. Do you know of a good walkthrough? Thanks much!

I always wanted to do this. Will try out this trick now. Thanks.

Write a comment

*