3

I am trying to access ACF data from another page to be displayed on another using Timber (Twig).

The ACF name is the_unstrung_hero in the "About" page (id = 7).

page-home.php:

<?php
$context = Timber::get_context();
$post = new TimberPost();
$about_page_id = 7;
$about = new TimberPost($about_page_id);
$about->acf = get_field_objects($about->ID);
$context['post'] = $post;
Timber::render( array( 'page-' . $post->post_name . '.twig', 'page.twig' ), $context );

Within page-home.twig:

<p>{{ acf.the_unstrung_hero|print_r }}</p>

This is just the last combination attempt of many. Frankly I am just not getting something (PHP is not a forte of mine)... Your help will be greatly appreciated.

  • Have you simple tried using acf field id instead of name "the_unstrung_hero"? – Deepak jha Apr 18 '16 at 21:21
  • How would I get the ACF field "ID"? Thnx –  Apr 18 '16 at 21:25
  • While adding/editing ACF fields(field groups), click on "screen options", there you will see a checkbox "Field key". Check and and you will be able to see "Field keys"(Or IDs as I used it) on left side of field. – Deepak jha Apr 18 '16 at 21:31
  • I have tried Field Keys - Confused me because you said "field id". –  Apr 18 '16 at 21:35
  • Can you try this one `the_field('the_unstrung_hero',7);` – Deepak jha Apr 18 '16 at 21:40
  • That's a step forward. Adding `$context["acf"] = the_field('the_unstrung_hero',7);` to the php file adds the correct content to the top of the page. However that is not desirable - Needs to be injected page-home.twig at `

    {{ acf.the_unstrung_hero}}

    `
    –  Apr 18 '16 at 21:52
  • if you want value in `$context["acf"]` then don't use `the_field('the_unstrung_hero',7);`, instead use `get_field('the_unstrung_hero',7);` – Deepak jha Apr 18 '16 at 22:27
  • Thnx but that puts us back where I was (except we're calling on one object, not an array). The data is not printing to `

    {{ acf.the_unstrung_hero }}

    `
    –  Apr 18 '16 at 22:32

1 Answers1

5

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>
Community
  • 1
  • 1
Gchtr
  • 2,749
  • 2
  • 18
  • 28