CPT Loop by Category
- This topic has 5 replies, 2 voices, and was last updated 4 years ago by
[email protected].
-
AuthorPosts
-
May 21, 2021 at 3:22 PM #28354
[email protected]
ParticipantHi,
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
May 22, 2021 at 12:27 PM #28370Long Nguyen
ModeratorHi 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://support.metabox.io/topic/mb-view-by-shortcode/
May 25, 2021 at 3:55 PM #28407[email protected]
ParticipantHi,
I'm still a bit confused I'm not sure what I need to do.
Could you please show me an example.
Thanks.
May 25, 2021 at 4:08 PM #28409[email protected]
ParticipantUpdate,
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>
May 25, 2021 at 5:37 PM #28415Long Nguyen
ModeratorHi,
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://support.metabox.io/topic/displaying-two-cpts-in-custom-taxonomy-term/
May 25, 2021 at 6:11 PM #28417[email protected]
ParticipantHi,
Thanks for all your help that worked perfectly!
-
AuthorPosts
- You must be logged in to reply to this topic.