WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have created a custom meta box for posts and I need to display said data. I have been using the below method to display the data, But I am in need of a slightly different solution.

<a href="<?php echo esc_attr( get_post_meta( get_the_ID(), 'my_meta_box_text4', true ) ); ?>">
    <img src="<?php echo esc_attr( get_post_meta( get_the_ID(), 'my_meta_box_text3', true ) ); ?>" alt="" onerror="this.style.display='none'"/>

What I need is a way to do a IF/Else statement such is. If my_meta_box_text3 exists echo that, else echo this.

share|improve this question
up vote 0 down vote accepted

You can try this:

// get image source:
$metaboxtext3 = esc_attr( get_post_meta( get_the_ID(), 'my_meta_box_text3', true ) );

// get link:
$metaboxtext4 = esc_attr( get_post_meta( get_the_ID(), 'my_meta_box_text4', true ) );

// check if the image source exists:
if(strlen($metaboxtext3)>0){ ?>
   <a href="<?php echo $metaboxtext4; ?>"><img src="<?php echo $metaboxtext3; ?>" alt="" onerror="this.style.display='none'"/></a> 
<?php } ?>
share|improve this answer
Thanks this worked perfect! – Phil Hoyt Feb 6 '13 at 20:05
ok great ;-) (maybe you can mark it as answered) – birgire Feb 6 '13 at 21:44

Your Answer


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.