Take the 2-minute tour ×
WordPress Development Stack Exchange 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

7 Answers 7

$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

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

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

@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

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
$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 '13 at 12:38

Most of the above examples work but if you are using multiple categories NONE (as of writing, WP version 3.6) of the other methods work to get all the categories that have been passed to either "cat" or "category_name".

The only way I have found is to pull the data from:

$wp_query->query['category_name']

For some reason this returns a different value to get_query_var( 'category_name' ) which only returns the first category.

When using multiple categories you will have to use some function like explode to get an array of category slugs, then loop through that to grab all the IDs:

<?php
global $wp_query;

//grab all categories from query string (if using `category_name`)
$category_slugs_array = explode("+",esc_attr($wp_query->query['category_name']));

$categories = array();
$category_ids = array();

//loop through all the slugs
foreach($category_slugs_array as $category_slug)
{
    //get category object using slug
    $category = get_category_by_slug( $category_slug );

    //check to make sure a matching category has been found
    if(isset($category->cat_ID))
    {
        $categories[] = $category;
        $category_ids[] = $category->cat_ID;
    }
}

var_dump($categories); //array of categories
var_dump($category_ids); //array of category IDs

?>

Obviously there needs to be some considerations when using AND (+) or OR (,) operators.

share|improve this answer

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.