I have a content type called events. I created an image media type reference field called speaker image and set it to unlimited. Within my media image type, I've created two custom fields, a text field for caption, and a taxonomy term field to choose the speaker type ie: featured, guest, moderator, etc. I would like to use the taxonomy term id to create a class to wrap the image, and caption in so that I can style each one differently. For example, the Featured speaker image would be larger than the guest speaker and a few other things. I've thought of doing this with a preprocess function to no avail.

here is some code I've used in the twig file itself that hasn't worked -

{% if content.field_taxonomy.target_id == '7' %}
   do this
  {% else %}
   don't do that
{% endif %} 

Please help

  • The code you have should work, I tested it in a local D8 site I have. So something else might not be set right. For example the display settings. Make sure the field is set to display on that view mode. Also, what template are you trying to render this in? Have you tried debugging to see what is going on? – Danny Englander Sep 13 at 0:03

Your Answer

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

Browse other questions tagged or ask your own question.