php echo not working correctly with multiple statements... (2 posts)

  1. abductedrecords
    Posted 4 years ago #

    <?php if(get_post_meta($post->ID, "website", true)=='website');{
    							echo get_post_meta($post->ID, "website", true); }?>            
    						<?php if(get_post_meta($post->ID, "website", true)=='website'); {
    							echo '<a href="' . get_post_meta($post->ID, "website", true) . '">TESTLINK</a> •'; }?>

    Above i have 2 lines of code. Im trying to only display a link for a website when there is a website defined by the user. For some reason the secodn statement shows "TESTLINK" on every post, when the first one only displays the text of the website when there is one, and nothing when there isnt (which is what i want). Why does the 2nd code echo every time no matter what? thanks ahead of time for the help

  2. bcwp
    Posted 4 years ago #

    Remove the ; before the { on both if statement lines. The format for if statements in PHP is:

    <?php if ( $value == $variable ) { ?>
    <?php echo 'the condition was true'; ?>
    <?php else { ?>
    <?php echo 'the condition was false'; ?>
    <?php } ?>

    or, you can use : and endif; instead of curly braces {}, like so

    <?php if ( $value == $variable ) : ?>
    <?php echo 'the condition was true'; ?>
    <?php else : ?>
    <?php echo 'the condition was false'; ?>
    <?php endif; ?>

    Of course, you don't need to break in and out of <?php ?> code blocks on every line. I only did that for example purposes.

    Just don't mix and match the formats, and don't place a semicolon ; at the end of an if statement because it is not a function call.

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.