Unabled to show term in views

Support MB Views Unabled to show term in viewsResolved

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #38472
    JamesJames
    Participant

    HI, Metabox. Hope you can help me show a category using this query I have:

    {% set args = { post_type: "listing", posts_per_page: 7} %}
    {% set posts = mb.get_posts( args ) %}
    {% for post in posts %}
    
    <h3>{{ term.name }}</h3>
    
    {% endfor %}
    #38477
    Long NguyenLong Nguyen
    Moderator

    Hi Kirb,

    You are using the WP function get_posts() which means it will return posts only. If you want to get terms by post ID, please use the function get_the_terms()
    https://developer.wordpress.org/reference/functions/get_the_terms/

    Or if you want to create a term query to display terms only, please use the WP function get_terms()
    https://developer.wordpress.org/reference/functions/get_terms/

    #38482
    JamesJames
    Participant

    Hi, Long. Sorry, I'm having a hard time translating some php code to this twig template, can you give me a sample where I can show post title and its related category? Thank you.

    #38490
    Long NguyenLong Nguyen
    Moderator

    Hi,

    I think it's not really hard to do. Here is an example

    {% set args = { post_type: "listing", posts_per_page: 7} %}
    {% set posts = mb.get_posts( args ) %}
    {% for post in posts %}
        {{ post.title }}
    	{% set term_list = mb.get_the_terms( post.ID, 'category' ) %}
    
    	{% for term in term_list %}
    	    <div class="term-item {{ term.slug }}">
    	        {{ term.name }}
    	    </div>
    	{% endfor %}
    
    {% endfor %}
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.