Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

how do I get the cat ID inside the Template. Very important: I can not do it by the name, because we have muliple cats with the same name. Only the slug is different. If I'd get the slug, it would be okay, too. But like I said: I can not use the Cat title.....

share|improve this question

6 Answers

$wp_query->get_queried_object() will give you the "currently queried object". On a category archive this is the category object, on a author page this is the author, on a single post this is the post itself, ... well, you get the the idea. If you only want the ID you can also use $wp_query->get_queried_object_id().

share|improve this answer

Unless I am misunderstanding the question, I think you can also add the category id/slug to the body class:

<?php if(is_category()) { $cat_ID = 'cat-'.get_query_var('cat'); } ?>
<body <?php body_class($cat_ID); ?>>
share|improve this answer

Umm, I can't comment yet, but VicePrez's answer does work. The following works just fine on a category archive page (although you probably want to do something other than just echo it):

<?php
$category = get_the_category(); 
echo $category[0]->cat_ID;
?>

EDIT: Scratch that, it worked for me until I came across a category that didn't have a post, then it picked up the subcategory instead of the main category. You can't rely on get_the_category on a category template page.

share|improve this answer

You could use get_the_category() to do that.

Example:

<?php

$category = get_the_category(); 

// use this to echo the slug
echo $category[0]->slug;

// use this to echo the cat id
echo $category[0]->cat_ID;

// if you've got multiple categories you can run a foreach loop like so
foreach ( $category as $cat ) :

    echo '<li>' . $cat->name . '</li>';

endforeach;

?>

You could use:

<?php
    echo '<pre>';
    print_r($category);
    echo '</pre>';
?>

to view the array of objects that are returned.

share|improve this answer
the question 'how do I get the cat ID inside the Template' is open to different interpretation. within a single post template, you are right. it works inside the loop to get categories of a single post; however,get_the_category() will not work in a category archive page to get the category id, the result would be arbitrary. – Michael May 29 '11 at 21:00
@Michael true say. @Jan seems to have given a more appropriate answer in relation to that specific context. – VicePrez May 29 '11 at 21:16
1  
get_the_category() does work inside category.php – Lea Cohen Jul 5 '11 at 8:19

@Jan Fabry's response is actually the correct answer, here's why: Since Wordpress allows multiple categories for a post, using $category = get_the_category() and querying $category[0] will not work in every case since what you're actually doing is asking for the first category of the first post. Imagine you have categories A, B and C. If you have only one post, it has categories A and B and you're inside B's category page, you may end up with A's information instead.

That's why it's better to use $category = $wp_query->get_queried_object(), because in the previous example it will always get you B's information when you're inside B's category page.

share|improve this answer
$category = get_category( get_query_var( 'cat' ) );

$cat_id = $category->cat_ID;

$catname=explode(",",get_category_parents($cat_id,'',','));
print_r($catname);
share|improve this answer
1  
Please explain why that could solve the problem. A code snippet is not an answer. – toscho Jan 11 at 12:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.