WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] If function with multiple "answers" (8 posts)

  1. petersandeen
    Member
    Posted 1 year ago #

    Hi,

    Sorry about the title, I didn't know how to ask this more clearly/concisely. (I also tried to search for the answer but came up with nothing because I didn't quite know what I'm looking for...)

    I want to add several possible "answers" into an if-statement.

    For example: if(in_category('birds' OR 'mammoths')):

    Is it even possible to add multiple possibilities? I know I can add multiple elseif-statements and get the same result, but I'd like to keep the code shorter...

    Help is much appreciated :)

  2. IshaanRawat
    Member
    Posted 1 year ago #

    yes...

    if(in_category(birds)) {
    echo "sdada"
    }
    
    elseif (in_category(mammoths)) {
    echo "sadasd"
    }
    
    elseif(......) {.....}
    
    .....

    YOu can add as many else if statements as you want...

  3. petersandeen
    Member
    Posted 1 year ago #

    Thank you Ishaan for your reply.

    But as I mentioned in the original question, I know I can add else if statements, but I'd like to add more "answers" into the first if statement to keep the code shorter. There will be more than 20 else if statements (all with a bunch of code) if I go that road.

    If it's not possible to have multiple answers in one if statement, I'll have to use multiple else if's...

  4. IshaanRawat
    Member
    Posted 1 year ago #

    so this is what you want (|| is used for OR) --->

    if(in_category("birds" || "mammoths" || "animals")) {
    echo "dasdasda"
    }
  5. jnhghy
    Member
    Posted 1 year ago #

    Ok so this is possible:
    if(in_category('birds')|| in_category('mammoths')):
    you can learn more about it here or here
    Have a nice one

  6. petersandeen
    Member
    Posted 1 year ago #

    Thank you so much Ishaan :)

    That's exactly what I was looking for.

    Out of curiosity, is it possible to combine two different functions in one if statement? In other words: if(in_category('birds') OR has_tag('mammoths')):

  7. petersandeen
    Member
    Posted 1 year ago #

    Thank you jnhghy, you answered before I even could ask... :D

  8. IshaanRawat
    Member
    Posted 1 year ago #

    Your welcome

Topic Closed

This topic has been closed to new replies.

About this Topic