Forums

[resolved] Combining two conditions (9 posts)

  1. robcub
    Member
    Posted 1 year ago #

    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.

  2. 123milliseconds
    Member
    Posted 1 year ago #

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

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

  3. robcub
    Member
    Posted 1 year ago #

    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.

  4. robcub
    Member
    Posted 1 year ago #

    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

  5. 123milliseconds
    Member
    Posted 1 year ago #

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

    need one more bracket at end

  6. robcub
    Member
    Posted 1 year ago #

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

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

  7. robcub
    Member
    Posted 1 year ago #

    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.

  8. robcub
    Member
    Posted 1 year ago #

    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.

  9. robcub
    Member
    Posted 1 year ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic