Custom Fields
Re-Envisioned
Support MB Views CPT Loop by Category

  • Creator
    Topic
  • #28354
    Resolved info@insigniacreative.co.uk
    Participant

    Hi,

    I'm trying to build a view based on CPT that only pulls from a set category but the args ignore both the CPT and the category this is what I have so far:

    {% set args = {post_type: 'project', posts_per_page: 10, cat: 16 } %}
    
    <div class="sidebarnews">
    {% set posts = mb.get_posts( args ) %}
    <ul>
        {% for post in posts %}
            <li><img src="{{ post.thumbnail.medium.url }}" width="{{ post.thumbnail.medium.width }}" height="{{ post.thumbnail.medium.height }}" alt="{{ post.thumbnail.medium.alt }}">
    {% for item in post.logo %}
        <img src="{{ item.medium.url }}" width="{{ item.medium.width }}" height="{{ item.medium.height }}" alt="{{ item.medium.alt }}">
    {% endfor %}</li>
        {% endfor %}
    </ul>
    </div>

    Alan

Viewing 5 replies - 1 through 5 (of 5 total)
  • Author
    Replies
  • #28370
    Long Nguyen
    Moderator

    Hi Alan,

    If you use the function get_posts(), it will return post objects, we can call it a custom query. A post object does not support to access the field ID as a property of it.

    You can get the post info via the post ID in a custom query by using the WordPress functions or our helper function rwmb_meta(). For example, featured image, use the function get_the_post_thumbnail().

    Refer to this topic https://metabox.io/support/topic/mb-view-by-shortcode/

    #28407

    Hi,

    I'm still a bit confused I'm not sure what I need to do.

    Could you please show me an example.

    Thanks.

    #28409

    Update,

    I've now got the images to show using this code below. Could you tell me how I can make it only pull the information from a certain post category.

    <div class="sidebarnews">    
        {% set args = {post_type: 'project', posts_per_page: 10} %}
        {% set posts = mb.get_posts( args ) %}
        {% for post in posts %} 
            {# Image #}
            {% set image = mb.rwmb_meta( 'logo_market', '', post.ID ) %}
            <img src="{{ image['full_url'] }} "> 
    
            {{ mb.get_the_post_thumbnail( post.ID, 'thumbnail' ) }}
        {% endfor %}    
    </div>
    #28415
    Long Nguyen
    Moderator

    Hi,

    You need to add the argument taxonomy to get posts from certain taxonomy.

    {% set args = { post_type: 'project', posts_per_page: 10, tax_query: [ { taxonomy: 'taxonomy-slug', field: 'slug', terms: 'term-slug' } ] } %}

    Refer to this topic https://metabox.io/support/topic/displaying-two-cpts-in-custom-taxonomy-term/

    #28417

    Hi,

    Thanks for all your help that worked perfectly!

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