Can Twig output vary based on value of taxonomy
- This topic has 4 replies, 2 voices, and was last updated 2 years, 3 months ago by
swest.
-
AuthorPosts
-
January 7, 2023 at 12:12 AM #40147
swest
ParticipantIn pseudo code
Based on the value of the honor_type taxonomy, I would like different output:
if Honor_Type = "Faculty"
output <h4 class="class-of">Years of Service {{ post.vod_class_of_tax.name }}</h4>
else
output <h4 class="class-of">Years of Service {{ post.vod_class_of_tax.name }}</h4>
end ifIs this possible with Twig? If so, how? Here's the Twig code I have in the View: <div class="card-heading"> <h4 class="honor-type"> {% for item in post.vod_honor_type_tax %} {{ item.name }} {% endfor %}</h4> <h1 class="honoree-name">{{ post.vod_honoree_names }}</h1> <h4 class="class-of">class of {{ post.vod_class_of_tax.name }}</h4> </div>
It's the last line containing the h4 that I need to vary based on the value of vod_honor_type.
Thanks for any help you can give.
January 8, 2023 at 7:45 AM #40155Peter
ModeratorHello there,
It's not a possibility of Twig, it's how you create your conditional code to output the value. I add the code below for example.
{% for item in post.vod_honor_type_tax %} {% if item.name == 'somthing' %} <h4 class="honor-type">{{ item.name }}</h4> <h1 class="honoree-name">{{ post.vod_honoree_names }}</h1> <h4 class="class-of">class of {{ post.vod_class_of_tax.name }}</h4> {% else %} something if false {% endif %} {% endfor %}
To understand how to use Twig, please read more on the documentation https://docs.metabox.io/extensions/mb-views/#twig
January 9, 2023 at 2:47 AM #40161swest
ParticipantThanks Peter.
This code goes into a view for a Single item, so I don't think I can use a for loop as I'm seeing multiple items returned.
<div class="vod-wrapper"> <div class="card-heading"> <h4 class="honor-type">{{ post.vod_honor_type_tax.name }}</h4> <h1 class="honoree-name">{{ post.vod_honoree_names }}</h1> {% for item in post.vod_honor_type_tax %} {% if item.name == 'Faculty' %} <h4 class="years-of-service">Years of Service {{ post.years_of_service }}</h4> {% else %} <h4 class="class-of">Class of {{ post.vod_class_of_tax.name }}</h4> {% endif %} {% endfor %} </div>
With the View code above, I'm seeing this for output
https://share.cleanshot.com/W9vpCDL2
and
https://share.cleanshot.com/bhN2v6XjInstead of this:
https://share.cleanshot.com/pKqwJ0gQ
and
https://share.cleanshot.com/4vVBzhZ1January 9, 2023 at 11:31 PM #40170Peter
ModeratorHello,
If this code output the honor type term name correctly
<h4 class="honor-type">{{ post.vod_honor_type_tax.name }}</h4>
then I think you don't need to use the loop to check the term name.
January 9, 2023 at 11:47 PM #40172swest
ParticipantThank you Peter.
This is what I finally got to work. I appreciate your help in finding a solution.The key I was looking for was:
{% if post.vod_honor_type_tax.name == 'Faculty' %}
<div class="card-heading"> <h4 class="honor-type">{{ post.vod_honor_type_tax.name }}</h4> <h1 class="honoree-name">{{ post.vod_honoree_names }}</h1> {% if post.vod_honor_type_tax.name == 'Faculty' %} <h4 class="years-of-service">Years of Service {{ post.years_of_service }}</h4> {% else %} <h4 class="class-of">class of {{ post.vod_class_of_tax.name }}</h4> {% endif %} </div>
-
AuthorPosts
- You must be logged in to reply to this topic.