• Resolved marcc1

    (@marcc1)


    Hi, how do I get the category (ID) of the actual displayed single or category page?

    I only define ONE category to each article!

Viewing 4 replies - 1 through 4 (of 4 total)
  • In a category template:

    $cat = get_query_var('cat');
    echo $cat;

    In a single type template and in The Loop see example in the article get_the_category.

    Thread Starter marcc1

    (@marcc1)

    okay thanks

    I now get the cat ID using this piece of code:

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

    but this

    if ($category[0]->cat_ID = 113) {
    ...
    } elseif ($category[0]->cat_ID = 122) {
    ...
    }

    wont work..

    Thread Starter marcc1

    (@marcc1)

    kay thanks

    this works:

    $category = get_the_category();
    $myCat = $category[0]->cat_ID;
    if ($myCat == 113) {
    ...
    } elseif ($myCat == 122) {
    ...
    }

    Its because you arent using an operator, you are checking to see if the category was assigned. This is what you should be doing:

    $category = get_the_category();
    if ( $category[0]->cat_ID == 3 )
    {
    	soSomething();
    }

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘how to get the active post’s category’ is closed to new replies.