Hide MB View if No Content

Support MB Views Hide MB View if No ContentResolved

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

    In a Metabox View that displays related posts (set using relationship), how can I have the view not display if there are no related posts set? The MB View is being added to a custom post template using an MNB Views shortcode. It includes a heading, and even if there are no related posts, the heading is currently displaying. I want the whole view (including the headline) to not display if there are no related posts.

    #36883
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Simply, you can create an if state to check if there is a related post and display the heading. For example

    {% if (condition here) %}
        heading here
        {% for post in relationship.to %}
    	
        {% endfor %}
    {% endif %}
    #37000
    Brent WilsonBrent Wilson
    Participant

    Can you help me figure out what condition I should be adding for the if statement?

    #37014
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Here is an example to check if there is a related post then display the heading.

    {% set relationship = attribute( relationships, 'post-to-page' ) %}
    {% set posts = relationship.to %}
    
    {% if posts is not empty %}
    	My heading here
    	{% for post in posts %}
    		{{ post.title }}
    	{% endfor %}
    {% endif %}
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.