Custom post type query with twig, post link

Support MB Views Custom post type query with twig, post linkResolved

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #19863
    JasonJason
    Participant

    Using 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>
    
    #19871
    Long NguyenLong Nguyen
    Moderator

    Hi Jason,

    To get the post permalink, please use the function get_permalink() and call it via mb proxy. 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 post object.
    https://codex.wordpress.org/Class_Reference/WP_Post

    #19880
    JasonJason
    Participant

    That works! Thank you!

    #19881
    JasonJason
    Participant

    Now 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 %}
    
    #19888
    Long NguyenLong Nguyen
    Moderator

    Hi,

    You can use the function get_the_post_thumbnail_url() to get the post thumbnail URL and call it via mb proxy. 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/.

    #19921
    JasonJason
    Participant

    Thank you! That works!

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