0

I have the following code which is trying to show an image based on the value selected from a custom field in the admin screen.

 <?php if( get_field('bias') != 'Neutral' )  { ?>
<img src="https://www.streetfiresite.com/wp-content/themes/streetfiresite/img/twoarrows-nuetral.png" />
<?php } elseif ( get_field('bias') != 'Bullish' ){ ?>
<img src="https://www.streetfiresite.com/wp-content/themes/streetfiresite/img/bullish.png" />
<?php } elseif ( get_field('bias') != 'Bearish' ){ ?>
<img src="https://www.streetfiresite.com/wp-content/themes/streetfiresite/img/bearish.png" />
<?php } else { ?>
<?php } ?>

The above only kind of works. It shows the bear image for the neutral value and the neutral image for the bullish and bearish values. I really don't need the 3 options. I think my code is just a little off somewhere. Can anyone help?

P.S. I'm using Advanced Custom Fields.

0

You have a logical error.

You check if the field is != and not ==. So if the field name is Neutral the first check will fail and it will go to the second one and it will succeed.

You ask is Neutral not equal to Neutral and the answer is false because Neutral is equal to Neutral.

The same for the other ones.

<?php if( get_field('bias') == 'Neutral' )  { ?>
<img src="https://www.streetfiresite.com/wp-content/themes/streetfiresite/img/twoarrows-nuetral.png" />
<?php } elseif ( get_field('bias') == 'Bullish' ){ ?>
<img src="https://www.streetfiresite.com/wp-content/themes/streetfiresite/img/bullish.png" />
<?php } elseif ( get_field('bias') == 'Bearish' ){ ?>
<img src="https://www.streetfiresite.com/wp-content/themes/streetfiresite/img/bearish.png" />
<?php } else { ?>
<?php } ?>
  • Thanks you @Laxmana. I did not realize that. Your solution solved my problem. – streetfire Jan 6 '16 at 22:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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