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 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'"/>
</a>

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

1 Answer 1

up vote 0 down vote accepted

You can try this:

<?php  
// 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

 
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.