The MU forums have moved to WordPress.org

Conditional code in sidebar based on category_id (3 posts)

  1. pschena
    Member
    Posted 14 years ago #

    Trying to do what I thought would be pretty simple.
    Wanted to add some conditional code based on the post category to the sidebar.

    Code I am using is:

    <?php global $category_id;
    if ($category_id == 259) {echo 'YYY'; }
    else {echo 'NNN'; }; ?>

    Does not seem to be working correctly.
    Not sure where I am going wrong.
    Anyone see what I'm missing?

  2. Driftless1
    Member
    Posted 14 years ago #

    Not sure that you've actually got anything in your variable $category_id

    Try using the is_category() function inside a loop... or in_category()

    <?php if (in_category('259')
        echo 'YYY';
    else
        echo 'NNN';
    ?>

    http://codex.wordpress.org/Template_Tags/in_category

  3. pschena
    Member
    Posted 14 years ago #

    Thanks Driftless1.

    Ended up with a plugin work around.
    I think the problem was that it wasn't in the loop.

    // closed

About this Topic