C Dog
C Dog

Reputation: 157

How to add if statement

Need some help with trying to add on an if statement to the following code.

// determine email
$email = '<a href="mailto:' . get_post_meta($post->ID, 'resource_email', true) . '">'. get_post_meta($post->ID, 'resource_email', true) .'</a>';
$tpl = str_replace( '%%EMAIL%%', $email, $tpl );

What I would like to do, is this:

If there's an email display it like this...

<p><i class="icon"></i><a href="mailto:' . get_post_meta($post->ID, 'resource_email', true) . '">'. get_post_meta($post->ID, 'resource_email', true) .'</a><p>

If there's not email, display nothing.

Not sure how to do this, I've tried this, but failed :-)

// determine email
$email = '<a href="mailto:' . get_post_meta($post->ID, 'resource_email', true) . '">'. get_post_meta($post->ID, 'resource_email', true) .'</a>';
if ($email) {
    '<i class="ss-mail"></i><h6>Email</h6><p>';
    $tpl = str_replace( '%%EMAIL%%', $email, $tpl );
    '</p>';
}

Thanks

Upvotes: 0

Views: 67

Answers (2)

Subharanjan
Subharanjan

Reputation: 668

$resource_email = get_post_meta($post->ID, 'resource_email', true);
if( !empty( $resource_email ) ) {
    $email = '<a href="mailto:' . $resource_email . '">'. $resource_email .'</a>';
    $tpl = str_replace( '%%EMAIL%%', $email, $tpl );
    echo '<i class="ss-mail"></i><h6>Email</h6><p>';
    echo $tpl;
    echo '</p>';
} else {
    $email = ' ';
    $tpl = str_replace( '%%EMAIL%%', $email, $tpl );
    echo $tpl;
}

Upvotes: 0

wunderdojo
wunderdojo

Reputation: 1027

You can just wrap it in an if statement since get_post_meta() won't return a result if there isn't an email address. So:

if(get_post_meta($post->ID, 'resource_email',true)){
.../your existing code ...
}

Upvotes: 1

Related Questions