• Resolved Rob Cubbon

    (@robcub)


    Hi,

    I’ve found myself with a WP challenge where I have to display something different depending on two categories and whether the user is logged in or not.

    So there are 5 different permutations of delivering information. This is inside the loop on a custom page template.

    If is in category “fruit” and user is logged in
    do this

    If is in category “fruit” and user is logged out
    do this

    If is in category “veg” and user is logged in
    do this

    If is in category “veg” and user is logged out
    do this

    elseif (is in another category other that fruit or veg and irrespective of whether they’re logged in or not)
    do this

    For categories I use:

    <?php if (in_category('veg')) { ?>

    For user logged in I use

    <?php if (is_user_logged_in() ) { ?>

    But I can’t combine the two.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Alwyn Botha

    (@123milliseconds)

    <?php if (in_category(‘veg’)) && (is_user_logged_in() ) { ?>

    http://php.net/manual/en/control-structures.if.php

    Thread Starter Rob Cubbon

    (@robcub)

    Thank you, 123milliseconds, I need to spend more time on php.net – I’ll see how I get on with this and post the code later.

    Thread Starter Rob Cubbon

    (@robcub)

    I’m sure I’m doing something really stupid but with

    <?php if (in_category('veg') && (is_user_logged_in() ) { ?>

    I’m getting: Parse error: syntax error, unexpected ‘{‘

    And with

    <?php if (in_category('veg')) && (is_user_logged_in() ) { ?>

    I’m getting: Parse error: syntax error, unexpected T_BOOLEAN_AND

    Alwyn Botha

    (@123milliseconds)

    <?php if ( in_category(‘veg’) && ( is_user_logged_in() ) ) { ?>

    need one more bracket at end

    Thread Starter Rob Cubbon

    (@robcub)

    Actually, before you posted I found you could do it with less brackets

    <?php } elseif (in_category('veg') && is_user_logged_in() ) { ?>

    Thread Starter Rob Cubbon

    (@robcub)

    It actually worked out that there were only 3 permutations necessary as there are only 2 categories in this loop and I only wanted one thing to happen if users weren’t logged in but these multiple conditionals are so, so powerful…

    <?php if ( in_category('veg')   && ( is_user_logged_in() ) )  { ?>
    Do this
    <?php } elseif (in_category('current-loan') && ( is_user_logged_in() ) ) { ?>
    Do this
    <?php } else { ?>
    Do that
    <?php } ?>

    Thank you, 123milliseconds, it never ceases to amaze me the quality advice I get here.

    Thread Starter Rob Cubbon

    (@robcub)

    Actually, I do need to put in some extra conditions.

    This works:

    <?php if ( in_category('veg')   && ( is_user_logged_in() ) )  { ?>
    Do this
    <?php } elseif (in_category('fruit') && ( is_user_logged_in() ) ) { ?>
    Do this
    <?php } else { ?>
    Do that
    <?php } ?>

    But, when I try adding if user isn’t logged in this doesn’t:

    <?php if ( in_category('veg')   && ( is_user_logged_in() ) )  { ?>
    Do this
    <?php } elseif (in_category('fruit') && ( is_user_logged_in() ) ) { ?>
    Do this
    <?php if ( in_category('veg')   && ( !is_user_logged_in() ) )  { ?>
    Do this
    <?php } elseif (in_category('fruit') && (! is_user_logged_in() ) ) { ?>
    Do this
    <?php } else { ?>
    Do that
    <?php } ?>

    It gives me Parse error: syntax error, unexpected T_ENDWHILE the endwhile is at the end of the loop, not here.

    Thread Starter Rob Cubbon

    (@robcub)

    My bad. Obviously, it should be

    <?php if ( in_category('veg')   && ( is_user_logged_in() ) )  { ?>
    Do this
    <?php } elseif (in_category('fruit') && ( is_user_logged_in() ) ) { ?>
    Do this
    <?php } elseif  ( in_category('veg')   && ( !is_user_logged_in() ) )  { ?>
    Do this
    <?php } elseif (in_category('fruit') && (! is_user_logged_in() ) ) { ?>
    Do this
    <?php } else { ?>
    Do that
    <?php } ?>

    Sorry!

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Combining two conditions’ is closed to new replies.