WordPress.org

Support

Support » How-To and Troubleshooting » How do you write an elseif statement?

How do you write an elseif statement?

  • Sorry this may be a very dumb question but how can I add an elseif to this:

    <?php if (is_page(10) || $post->post_parent==”10″) { ?>

    <p>Apples</p>

    <?php } else { ?>

    <p>Oranges</p>

    <?php } ?>

Viewing 2 replies - 1 through 2 (of 2 total)
  • ...
    <p>Apples</p>
    
    <?php } elseif (/* some condition */) { ?>
    
    <p>Pears</p>
    
    <?php } else {
    ...
    ?>

    See http://www.php.net/manual/en/control-structures.elseif.php

    See also switch

    Another note…

    When you are using if() or for() or any other contorl statement in a template file, it can be eaiser to read and debug if you use the alternative syntax. As an example:

    <?php if ($val == 1): ?>
      <p>Apples</p>
    <?php elseif ($val == 2): ?>
      <p>Oranges</p>
    <?php else: ?>
      <p>Pears</p>
    <?php endif; ?>

    It means the same thing, but can make it eaiser to read in template code.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How do you write an elseif statement?’ is closed to new replies.