Custom post type query with twig, post link
- This topic has 5 replies, 2 voices, and was last updated 5 years, 4 months ago by 
Jason.
 
- 
		AuthorPosts
 - 
		
			
				
May 28, 2020 at 6:02 AM #19863
Jason
ParticipantUsing the MB Views extension, I'm looping through a custom post type with the twig template engine. I'm not able to access the post url. Here is my code.
<div id="services" class="center mw8 pa3 f3 lh-copy"> <div class="flex flex-column flex-row"> {% set args = { post_type: "service", posts_per_page: 6 } %} {% set posts = mb.get_posts( args ) %} {% for key, post in posts %} {% if key % 2 == 0 %} <section> {% endif %} <div class="pv3 ph2"> <span class="ttu b f3 brand-primary bg-brand-secondary pa1 w-80 db">{{ post.post_title }}</span> <p>{{ post.post_content|slice( 0, 130) }}...</p> <a class="f4 b link dim br1 ph3 pv2 mb2 dib hover-brand-secondary brand-secondary bg-brand-primary" href="{{ post.post_link }}">Read More</a> </div> {% if key % 2 != 0 %} </section> {% endif %} {% endfor %} </div> </div>May 28, 2020 at 10:48 AM #19871Long Nguyen
ModeratorHi Jason,
To get the post permalink, please use the function
get_permalink()and call it viambproxy. The code should be{{ mb.get_permalink( post.ID ) }}.For more information, please follow the documentation
https://developer.wordpress.org/reference/functions/get_permalink/and variables (properties) we can use with
postobject.
https://codex.wordpress.org/Class_Reference/WP_PostMay 28, 2020 at 8:15 PM #19880Jason
ParticipantThat works! Thank you!
May 28, 2020 at 11:13 PM #19881Jason
ParticipantNow I'm not able to get the thumbnail url. Here is my code. Could you help me?
{% set args = { post_type: "project", posts_per_page: 6 } %} {% set posts = mb.get_posts( args ) %} {% for post in posts %} <a class="swiper-slide bg-moon-gray brand-secondary link" href="{{ mb.get_permalink( post.ID ) }}" style="background-image:url({{ post.thumbnail.thumbnail.url }})" > <span class="pt6 pb5 mt5 b">{{ post.post_title }}</span> </a> {% endfor %}May 29, 2020 at 9:00 AM #19888Long Nguyen
ModeratorHi,
You can use the function
get_the_post_thumbnail_url()to get the post thumbnail URL and call it viambproxy. The code should be{{ mb.get_the_post_thumbnail_url( post.ID ) }}.For more information, please follow the documentation https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/.
May 29, 2020 at 8:44 PM #19921Jason
ParticipantThank you! That works!
 - 
		AuthorPosts
 
- You must be logged in to reply to this topic.