Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I currently display a URL from a custom meta box, but wanted to add a conditional check, so that if nothing has been entered, not to display the link text.

<a target="_blank" href="<?php $urlbox = get_url_desc_box(); echo $urlbox[0]; ?>">Visit Website &raquo;</a>

At the moment, even when nothing is added to the URL Meta BOX, the Visit Website text still appears within the post.

share|improve this question

2 Answers 2

up vote 0 down vote accepted
$urlbox = get_url_desc_box();

if ( !empty( $urlbox[0] ) ) {
    echo sprintf( '<a target="_blank" href="%s">Visit Website &raquo;</a>', $urlbox[0] );
}
share|improve this answer
    
Thank you Ryan - perfect. –  Huw Rowlands Sep 27 '12 at 9:01

You should just need to move your logic outside of the <a> tag, and create a condition to only display if the result isn't null

<?php 
$urlbox = get_url_desc_box();
if ($urlbox[0] != null) { ?>
      <a target="_blank" href="<?php echo $urlbox[0]; ?>">Visit Website &raquo;</a>
<?php } ?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.