Show text depending on the category name using MB view

Support MB Views Show text depending on the category name using MB viewResolved

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #27493
    JamesJames
    Participant

    Hi, I would like to use the if statement using the MB view. I want to show a text depending on the category name. For example, the post category is == bootstrap then shows something to display. I tried it but I got an error. It works well in the other fields but I got an error work when using the category/term.

    Notice: Undefined index: term_id in /home/kirbkobz/public_html/wp-content/plugins/meta-box-aio/vendor/meta-box/mb-views/src/Fields/Taxonomy/Renderer.php on line 14

    {% for post in query.posts %}
    <div class="snippet-card">
        
        <a href="{{ post.url }}">
            <h3>{{ post.title }}</h3>
        </a>
        <p>{{ post.date | date( 'd M Y' ) }}</p>
        
        <!-- show when the category is == to bootstrap    -->
        {% if term.taxonomy == "bootstrap" %}
            <p>Bootstrap</p>
        {% else %}
        <p>No Bootstrap</p>
        {% endif %}
        
    </div>
    {% endfor %}
    #27527
    Long NguyenLong Nguyen
    Moderator

    Hi,

    The Terms tab in the Insert Field section is to use when the View Type is Archive > Location: Taxonomy Archive, if it set to Single, you should use the Post tab. Screenshot https://share.getcloudapp.com/o0u6Rm7J

    To get the post category, you can use the WP function get_the_category() via the proxy mb.

    Here is the sample code

    {% for post in query.posts %}
    <div class="snippet-card">
        
        <a href="{{ post.url }}">
            <h3>{{ post.title }}</h3>
        </a>
        <p>{{ post.date | date( 'd M Y' ) }}</p>
        <p>
    
        </p>
        <!-- show when the category is == to bootstrap    -->
        {% set post_categories = mb.get_the_category( post.ID ) %}
        {% for post_category in post_categories %}
            {% if post_category.name == "bootstrap" %}
                <p>Bootstrap</p>
            {% else %}
            <p>No Bootstrap</p>
            {% endif %}
        {% endfor %}    
    </div>
    {% endfor %}
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.