0

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.

1 Answer 1

0

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 } ?>
1
  • ok great ;-) (maybe you can mark it as answered)
    – birgire
    Feb 6, 2013 at 21:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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