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.com/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
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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 %}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0