1

I've set up a select field labeled "asset_type" with two values: "image" and "video". I then have two fields that rely on conditional logic to be displayed. If "image" is selected from the select I show the "image_asset" field, and if "video" is selected from the select I show the "video_asset" field. Reading through the docs and a few other stack questions, I thought I had the logic setup correctly, but for the life of me can not get the content to display. This is what my code looks like:

<?php if(get_sub_field('asset_type') == "image") { ?>

    <div><?php the_sub_field('image_asset'); ?></div>

<?php } ?>

<?php if(get_sub_field('asset_type') == "video") { ?>

    <div><?php the_sub_field('video_asset'); ?></div>

<?php } ?>

Any help or advice is greatly appreciated, thanks!

  • did you try to echo get_sub_field('asset_type') to see whats returning? are you using repeater fields? otherwise, don't you want to use get_field() instead? – pcarvalho Apr 19 '15 at 23:14
  • @peteroak thanks for replying! This block actually resides inside of a repeater field, and that's why I'm using sub_field. Echoing get_sub_field('asset_type') returns Image as text. Hmmm. – Darren Apr 19 '15 at 23:18
  • on the good track then! well, thats it then: == "Image" – pcarvalho Apr 19 '15 at 23:20
  • 1
    in case you didn't see the edit, you its capital I instead of i – pcarvalho Apr 19 '15 at 23:23
  • HAH! That did it @peteroak. I can't believe I missed something so trivial. Capitalizing Image and Video in the PHP and all is working as expected. Thank you so much for your help. – Darren Apr 19 '15 at 23:28
1

Just for the sake of closing the question:

<?php if(get_sub_field('asset_type') == "Image") { ?>
  <div><?php the_sub_field('image_asset'); ?></div>
<?php } ?>
<?php if(get_sub_field('asset_type') == "Video") { ?>
  <div><?php the_sub_field('video_asset'); ?></div>
<?php } ?>
| improve this answer | |
0

If you wanted to use the value of your select field instead of the label in your statement you can do this in ACF:

image : Image
video : Video

And this in your template (using the value not the human readable label):

<?php if(get_sub_field('asset_type', '') == "image") { ?>
  <div><?php the_sub_field('image_asset'); ?></div>
<?php } ?>
<?php if(get_sub_field('asset_type', '') == "video") { ?>
  <div><?php the_sub_field('video_asset'); ?></div>
<?php } ?>

Here get_sub_field('asset_type') has been changed to get_sub_field('asset_type', '')

This is a much cleaner option when faced with long labels e.g.

video : First Event Video 1920x1080

It also allows you to quickly change the label for the user without having to rework your code.

| improve this answer | |

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.