WordPress.org

Ready to get started?Download WordPress

Forums

is_category OR in_category condition (8 posts)

  1. somlor
    Member
    Posted 7 years ago #

    I'm trying to make a condition that creates sidebar content if a specific category archive is being viewed OR if a post in that category is being viewed.

    I was thinking this would work but it's not:

    <?php
    $post = $wp_query->post;
    if ( in_category(19) || is_category(19) ) : ?>

    DO SOME STUFF

    <?php endif; ?>

    Any suggestions?

    Thanks,
    Sean

  2. BPartch
    Member
    Posted 7 years ago #

    Hello

    <?php if (is_category('19') || in_category('19')) { ?>
    DO SOME STUFF
    <?php } ?>

    That should work! :)

  3. somlor
    Member
    Posted 7 years ago #

    That worked! Thanks. :)

  4. Jauhari
    Member
    Posted 7 years ago #

    I want no just one category that not show? how to do that? that I mean, I have 15 and 19 category, and I don't want this category show up in front my page.

    How to do that?

  5. acousticdryad
    Member
    Posted 7 years ago #

    <?php if ( !(in_category('29')) ) { ?>

    <?php } ?>

    Should exclude category 29.

  6. PaulWeber
    Member
    Posted 7 years ago #

    Interesting.
    Is there anything like an "AND" condition?

    I.e. if variable 1 AND variable 2 are true, then display content X
    else if variable 3 AND variable 4 are true, then display content Y?

    Thank you - it would be precious help!

    Paul Weber

  7. aaronsportfolio
    Member
    Posted 6 years ago #

    Paul, here is a solution:

    <?php
    
    if (is_category('xyz')) {
            Do something;
            }
    elseif (category('xyz2')) {
            Do something else ;
            }
    elseif (category('xyz3')) {
            Do something else;
    		}
    else {
            Do something if none of the above are true;
            }
    ?>

    - is_category can be replaced with any other conditional tag, ex: is_single('some single post') OR is_post('some post')

  8. smoczokwik
    Member
    Posted 6 years ago #

    Paul another option is

    <?php
    if ($1 == TRUE && $2 == TRUE) {

    echo "something";

    } elseif ($3 == TRUE && $4 == TRUE) {

    echo "something else";

    } else {

    echo "something completely different";

    }

    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic