Support » Developing with WordPress » PHP elseif statement problem

  • Resolved ratamatcat

    (@ratamatcat)


    This works

    <?php
    if( get_field('divergence', 10681) ) {
    	<div>Some text</div>
    }
    else
    	echo '<div>' . "Some alternative text" . '</div>';
    ?>

    But I want to use this method so the HTML isn’t echoed

    <?php if( get_field('divergence', 10681) ): ?>
    	<div>Some text</div>
    	<?php elseif; ?>
    	<div>Some alternative text</div>
    <?php endif; ?>

    I can’t get the else statement to work, only this works so far

    <?php if( get_field('divergence', 10681) ): ?>
    	<div>Some text</div>
    <?php endif; ?>

    I thought avoiding the echo method seems more appropriate when there’s a reasonable amount of HTML tags to use, as the above demo is simplified. Just want to place the tags as normal.

    Thanks,

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi.

    <?php if( get_field('divergence', 10681) ): ?>
    	<div>Some text</div>
    	<?php elseif; ?>
    	<div>Some alternative text</div>
    <?php endif; ?>

    In
    <?php elseif; ?>
    It should be
    <?php else: ?> (notice the “:”)

    A elseif, requires a new condition, like

    <?php if( get_field('divergence', 10681) ): ?>
    	<div>Some text</div>
    <?php elseif( get_field('otherfield', 10681) ): ?>
    	<div>Some otherfield text</div>
    <?php else: ?>
    	<div>Some alternative text</div>
    <?php endif; ?>
    • This reply was modified 1 month, 2 weeks ago by tagrelos.
    • This reply was modified 1 month, 2 weeks ago by tagrelos.

    This works great, thanks a ton! You really nailed it

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.