In your example above, I see that you get the field data from the about page, but you’re not adding it to the context. Your template doesn’t display that data, because you didn’t hand it over to the template.
You set up your context first:
$context = Timber::get_context();
Then you get the current post data that should be displayed:
$post = new TimberPost();
Now you did load $post
, but it’s not in your context yet. You have to put the data you want to display on your page into the $context
array. Then you render it trough Timber::render( 'template.twig', $context )
. Your Twig template will only contain data that is is present in $context
(to be complete: you can also use functions in your Twig templates to get data, but that is another topic).
To also add the data you loaded from the about page, you’d have to do this:
$about_page_id = 7;
$about = new TimberPost( $about_page_id );
$context['about'] = $about;
See that the line $about->acf = get_field_objects($about->ID)
is not there anymore? You don’t need it, because Timber automatically loads your ACF fields into the post data. Your field would now be accessible through {{ about.the_unstrung_hero }}
in your Twig template.
To come back to what you want to achieve:
I’d solve it like this.
Like Deepak jha mentionend in the comments of your question, I’d also make use the second parameter of the get_field()
function to get field data from a post by post ID.
You don’t really need to load the whole post of the about page if you just want to display the value of one ACF field.
page-home.php
$context = Timber::get_context();
$post = new TimberPost();
$context['post'] = $post;
// Add ACF field data to context
$about_page_id = 7;
$context['the_unstrung_hero'] = get_field( 'the_unstrung_hero', $about_page_id );
Timber::render( array( 'page-' . $post->post_name . '.twig', 'page.twig' ), $context );
And then in within page-home.twig you can access the field data in post.
<p>{{ the_unstrung_hero }}</p>
{{ acf.the_unstrung_hero}}
` – Apr 18 '16 at 21:52{{ acf.the_unstrung_hero }}
` – Apr 18 '16 at 22:32