WordPress.org

Ready to get started?Download WordPress

Forums

How do you write an elseif statement? (3 posts)

  1. celticks
    Member
    Posted 2 years ago #

    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 } ?>

  2. converting2wp
    Member
    Posted 2 years ago #

    ...
    <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

  3. michael.mariart
    Member
    Posted 2 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.