Forums

[resolved] how to get the active post's category (5 posts)

  1. Marcc1
    Member
    Posted 3 years ago #

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

    I only define ONE category to each article!

  2. MichaelH
    Member
    Posted 3 years ago #

    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.

  3. Marcc1
    Member
    Posted 3 years ago #

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

  4. Marcc1
    Member
    Posted 3 years ago #

    kay thanks

    this works:

    $category = get_the_category();
    $myCat = $category[0]->cat_ID;
    if ($myCat == 113) {
    ...
    } elseif ($myCat == 122) {
    ...
    }
  5. eTiger13
    Member
    Posted 3 years ago #

    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();
    }

Topic Closed

This topic has been closed to new replies.

About this Topic