Get Category ID inside Category template

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…..

Related posts

Leave a Reply

9 comments

  1. $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().

  2. 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.

  3. 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); ?>>
    
  4. @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.

  5. 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.

  6. $category = get_category( get_query_var( 'cat' ) );
    
    $cat_id = $category->cat_ID;
    
    $catname=explode(",",get_category_parents($cat_id,'',','));
    print_r($catname);
    
  7. 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.

  8. add_action( 'loop_start', 'demo_cat_id' );
    function demo_cat_id() {
    
    $terms = get_the_terms( get_queried_object_ID(), 'category' );
    
    $term = array_pop( $terms );
    
    echo $term->term_id;
    
    }
    

    You can also use get_the_terms inside your archive template or functions file and use the 2nd parameter to specify your taxonomy as a category.