I am new to Timber/Twig/PHP and have a fairly specific requirement. I have a page-home.php / page-home.twig which displays a couple of ACF fields, testimonials as a custom post type, and the latest 3 blog posts.
I'm mostly fine with the Twig frontend part, but having difficulties with setting up the php template files.
I've read the Timber docs and got the impression that ACF is baked in, so that the field can just be called directly in Twig. Also checked Github and SO for similar issues but did not find an answer. There is a video on youtube that covers custom post types, but it is within a loop of only posts.
This is my page-home.php
echo 'page-home.php';
$context = Timber::get_context();
$context['page'] = new Timber\Post();
$templates = array('page.twig');
if (is_front_page()){
$context['posts'] = Timber::get_posts('post_type=post&posts_per_page=3');
$context['testimonials'] = Timber::get_posts('post_type=testimonials');
// add your twig view to the front of the templates array
array_unshift($templates, 'page-home.twig');
}
Timber::render( $templates, $context );
$image = get_field('hero_image');
echo '<pre>';
var_dump( $image );
echo '</pre>';
page-home.php is showing 3 blog posts as expected, but returning a NULL value for the ACF field var_dump.
If I comment this line
// $context['posts'] = new Timber\PostQuery($args);
I get the var_dump of my image array, though it doesn't render in the Twig file.
This is the image in page-home.twig
<div class="image image-overlay" style="background-image:url({{ Image(post.meta('hero_image')).src }})"></div>
which returns blank.
Blog posts are being called as:
{% for post in posts %}
{% include "blogpost-card.twig" %}
{% endfor %}
I am also unsure how to call a separate custom post type (testimonials) on the same page since the documentation does not provide that example.
I'm using the starter-theme installed through Composer. It would be great if the theme or the documentation provided an example of a page containing posts, custom posts and ACF fields since I guess this is a very common scenario. At least for those like myself who need a little extra help with the PHP side.
array_unshift($templates, 'page-home.twig');
However the image still does not render, even though the var_dump returns data, and I still cannot get both posts and testimonials to appear. – Yan White$data['posts'] = Timber::get_posts('post_type=post&posts_per_page=3'); $data['testimonials'] = Timber::get_posts('post_type=testimonials');
and'for post in posts (or) testimonials'
– Yan White{{ dump(user) }}
in the Twig file I get data. But if I do {{ dump(post.hero_image) }} (or ..hero_image, or image) I get NULL. Will keep investigating and post the solution when I find it. I'm sure it will be something really obvious in hindsight.. – Yan White$post = new Timberpost()
to$post = new Timber\Post()
? What’s the return value for yourhero_image
in your ACF field settings? Can you make sure it returns an ID? What do you get when you only run{{ dump(post.meta('hero_image
)) }}` in Twig? – Gchtr