最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - How to make WPQuery custom post type work in TwigTimber

programmeradmin6浏览0评论

Im totally new to Twig and Timber. I was reading their doc and watch some tutorials however I cannot get it to work.

I have this WP Query with custom post type and ACF field:

    <?php
          $args = array( 'post_type' => 'what_our_clients_say', 'posts_per_page' => 2 );
          $loop = new WP_Query( $args );
          while ( $loop->have_posts() ) : $loop->the_post();
              echo '<div class="what-our-client-say-single-wrapper p-relative aos-init aos-animate" data-aos="fade-up" data-aos-delay="100">';
              echo '<div class="what-our-client-say-single-wrapper-violet"></div>';
                echo '<div class="what-our-client-say-inner-wrapper p-relative">';
                echo '<div class="what-our-client-say-inner-wrapper-img"><img src="' . get_field('what_our_clients_img') . '" class="img-client img-fluid"></div>';
                echo '<div class="what-our-client-say-inner-wrapper-all-txt"><div  class="what-our-client-say-inner-wrapper-header"><h3>'.get_the_title().'</h3></div>';
                echo '<div class="what-our-client-say-inner-wrapper-txt"><p>'.  get_the_excerpt() .'</p></div><div class="what-our-client-say-inner-wrapper-who"><p>'. get_field("what_our_clients_say_who") .'</p></div>';
                echo '</div></div><a href="; target="_blank" class="button big primary-colorbg no-hover what-our-client-say-special-btn">READ MORE';
                echo '</a>';
              echo '</div>';
          endwhile;
          wp_reset_postdata(); 
      ?>

And when I pass it to php file it works. However when I try to make it work in .twig file it doesn''t. My main problem is not with fields or excerpt / title parts (which I convert to: {{ post.title }} but with this loop:

 $args = array( 'post_type' => 'what_our_clients_say', 'posts_per_page' => 2 );
          $loop = new WP_Query( $args );
          while ( $loop->have_posts() ) : $loop->the_post();
(...)
   endwhile;
          wp_reset_postdata(); 

part. I would be very grateful for your help

Im totally new to Twig and Timber. I was reading their doc and watch some tutorials however I cannot get it to work.

I have this WP Query with custom post type and ACF field:

    <?php
          $args = array( 'post_type' => 'what_our_clients_say', 'posts_per_page' => 2 );
          $loop = new WP_Query( $args );
          while ( $loop->have_posts() ) : $loop->the_post();
              echo '<div class="what-our-client-say-single-wrapper p-relative aos-init aos-animate" data-aos="fade-up" data-aos-delay="100">';
              echo '<div class="what-our-client-say-single-wrapper-violet"></div>';
                echo '<div class="what-our-client-say-inner-wrapper p-relative">';
                echo '<div class="what-our-client-say-inner-wrapper-img"><img src="' . get_field('what_our_clients_img') . '" class="img-client img-fluid"></div>';
                echo '<div class="what-our-client-say-inner-wrapper-all-txt"><div  class="what-our-client-say-inner-wrapper-header"><h3>'.get_the_title().'</h3></div>';
                echo '<div class="what-our-client-say-inner-wrapper-txt"><p>'.  get_the_excerpt() .'</p></div><div class="what-our-client-say-inner-wrapper-who"><p>'. get_field("what_our_clients_say_who") .'</p></div>';
                echo '</div></div><a href="https://pragmaticbrains/case-study" target="_blank" class="button big primary-colorbg no-hover what-our-client-say-special-btn">READ MORE';
                echo '</a>';
              echo '</div>';
          endwhile;
          wp_reset_postdata(); 
      ?>

And when I pass it to php file it works. However when I try to make it work in .twig file it doesn''t. My main problem is not with fields or excerpt / title parts (which I convert to: {{ post.title }} but with this loop:

 $args = array( 'post_type' => 'what_our_clients_say', 'posts_per_page' => 2 );
          $loop = new WP_Query( $args );
          while ( $loop->have_posts() ) : $loop->the_post();
(...)
   endwhile;
          wp_reset_postdata(); 

part. I would be very grateful for your help

Share Improve this question asked Oct 7, 2020 at 12:52 DavidDavid 131 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I think is as simple as using Timber's Timber::get_posts() instead of the WP_Query class:

$context['testimonials'] = Timber::get_posts( ['post_type' => 'what_our_clients_say', 'posts_per_page' => 2] );
Timber::render('whatever.twig, $context);

... from there you can loop through the testimonials and access ACF values via meta:

{% for post in testimonials %}
    <h1>{{ post.attribution }} says:</h1>
    {# more here #}
{% endfor %}
发布评论

评论列表(0)

  1. 暂无评论