22

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

40

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

9

base on my search you must use this:

$category = get_queried_object();
echo $category->term_id;
  • I assume you received the downvote because you are not explaining why it helps, just stating that this does. You might want to inspect the source of that function and add your outcomings as edit to your answer. Hint: If a comment needs to be longer than your answer, then it should have been a comment :) – kaiser Feb 4 '17 at 12:44
7

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.

  • This is not a viable solution as this returns all the categories for all the posts on that category page. Your echoed result returns the initial category of the latest post in the list. – cj5 Nov 21 '17 at 17:34
4

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.

2

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.

  • 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
0
$category = get_category( get_query_var( 'cat' ) );

$cat_id = $category->cat_ID;

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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