Why Wont This Display?

Can’t figure out what I’m doing incorrectly here
this is at the top of the template file

<?php
/**
 * @package 1
 * @since 1 1.0
 */

$source_name = get_post_meta($post->ID, 'Source Name', true);
$source_url = get_post_meta($post->ID, 'Source URL', true);
?>

here is the other part thats further down:

Read More
<?php if($source_url) { ?>
<div id="content-source">
    <span>Source:</span> <a href="<?php echo $source_url; ?>"> <?php echo $source_name; ?></a>
</div>
<?php } ?>

If I remove <?php if($source_url) { ?> and <?php } ?> it works fine, but how do I get it to work so if theres no source nothing will be displayed?

Related posts

Leave a Reply

1 comment

  1. A quick look-up of the get_post_meta() function:

    If there is nothing to return the function will return an empty array unless $single has been set to true, in which case an empty string is returned.

    So, try:

        <?php if($source_url <> "") { ?>
        <div id="content-source">
            <span>Source:</span> <a href="<?php echo $source_url; ?>"> <?php echo $source_name; ?></a>
        </div>
        <?php } ?>
    

    Previous you were checking if nothing is returned. You need to be checking for an empty string instead.