How To Display Associated Custom Taxonomy Terms in Related Posts Query Loop

Support MB Views How To Display Associated Custom Taxonomy Terms in Related Posts Query LoopResolved

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #36869
    Brent WilsonBrent Wilson
    Participant

    I am setting up a site to display related presenters on an event post. The relationship is set up using MB Relationships. Along with a presenter's image and name, I want to be able to display the terms from a custom taxonomy that have been applied to that presenter (taxonomy slug is presenter-type). What bit of code do I need to use to pull the applied taxonomy terms from that custom taxonomy in MB Views? I would like the associated terms for the presenter-type taxonomy to be displayed with a " | " seperator.

    <h2 class="centered">
    	Event Presenter(s)
    </h2>
    
    <div class="presenters_container">
    {% set relationship = attribute( relationships, 'event-presenter-relationship' ) %}
    {% for post in relationship.to %}
    	<a href="{{ post.url }}">
    		<span class="presenter_card">
    			<img src="{{ post.thumbnail.medium_large.url }}" width="{{ post.thumbnail.medium_large.width }}" height="{{ post.thumbnail.medium_large.height }}" alt="{{ post.thumbnail.medium_large.alt }}">
    			<h3>
    				{{ post.title }}
    			</h3>
    			<p>
    					I WANT THE ASSOCIATED TERMS FROM THE PRESENTER-TYPE TAXONOMY DISPLAYED HERE, SEPERATED BY |
    			</p>
    		</span>
    	</a>
    {% endfor %}
    #36882
    Long NguyenLong Nguyen
    Moderator

    Hi Brent,

    You can use the WordPress function wp_get_post_terms() to get terms associated with a post based on the post ID. Please read more on the documentation https://developer.wordpress.org/reference/functions/wp_get_post_terms/

    For example:

    ...
    <h3>
    	{{ post.title }}
    </h3>
    <p>
    	I WANT THE ASSOCIATED TERMS FROM THE PRESENTER-TYPE TAXONOMY DISPLAYED HERE, SEPERATED BY |
    	{% set presenter_types = mb.wp_get_post_terms ( post.ID, 'presenter-type' ) %}
    	{% for presenter_type in presenter_types %}
    		{{ presenter_type.name }}
    	{% endfor %}
    </p>
    ...
    
    #37002
    Brent WilsonBrent Wilson
    Participant

    How would I add a vertical pipe "|" as a separator for cases where there are multiple presenter type terms?

    #37023
    Long NguyenLong Nguyen
    Moderator

    Hi,

    You can just add the separator after the code that outputs the term name

    {{ presenter_type.name }} |

    To get more details about how to use Twig with View, please follow the documentation https://docs.metabox.io/extensions/mb-views/#twig

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.