0

I am working in Wordpress trying to customize post layouts and am having trouble with a bit of php I've tried implementing.

I am trying to display an image in the post based on the value in a particular field in that post. For example, the field name is 'region' and I want to make it so if someone chooses 'NA' from that field then it displays one image in the post. If someone chooses 'EU' from that field then it will display another image in the post.

Below is the code that I am trying to use but for some reason it is not working and instead just displays both images for 'EU' and 'NA' at the same time.

    <?php if( get_field('region') == 'NA' )  { ?>
    <img src="https://www.esrnetwork.com/wp-content/uploads/2016/12/NA.png" width="25" height="25"/>
    <?php } elseif ( get_field('region') == 'EU' ){ ?>
    <img src="https://www.esrnetwork.com/wp-content/uploads/2016/12/EU.png" width="25" height="25"/>
    <?php } else { ?>
    <?php } ?>

I have very little coding experience and any help would be greatly appreciated.

1 Answer 1

1

How about this?

<?php $meta = get_post_meta( get_the_ID(), 'region', true );
    if ($meta == 'NA') { ?>
        <img src="https://www.esrnetwork.com/wp-content/uploads/2016/12/NA.png" width="25" height="25"/>
    <?php } elseif ($meta == 'EU') { ?>
        <img src="https://www.esrnetwork.com/wp-content/uploads/2016/12/EU.png" width="25" height="25"/>
  <?php } else {
        } ?>

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.