0

I'm trying to retrieve the user's metadata by first calling a custom field (using advanced custom fields plugin), using array values, the problem is that I could do that using PHP, but I have to use Timber because of my theme and there's not much info out there teaching how to use Timber and ACF (using advanced custom fields), even the info available is confusing and poor. I'm using Wordpress with Gantry5 framework and Helium theme.

First I set the custom field in ACF as "relational > user", then I set the data format as User Array, save, then I open a post and setup the fields inside the post, by choosing the user of each field.

So let's say the field name is "post_autor" and I need to display it below the post, the only problem is that I need to retrieve its arrays, so this is what I've tried to find the array values:

{% set author = post.get_field("post_autor") %} then {{ dump() }}

That line of code goes here: “themes/g5_helium/custom/views/partials/content-single.html.twig”, after this part:

{# Begin Page Content #}
                {{ post.paged_content|raw }}

                {{ function('wp_link_pages', {'before': '<div class="page-links" itemprop="pagination"><ul class="pagination-list">', 'after': '</ul></div>', 'link_before': '<span class="page-number page-numbers">', 'link_after': '</span>', 'echo': 0}) }}
                {# End Page Content #}

And this is the result I'm getting:

then array(1) { [0]=> array(11) { ["ID"]=> int(1) ["user_firstname"]=> string(7) "John" ["user_lastname"]=> string(5) "Doe" ["nickname"]=> string(4) "john" ["user_nicename"]=> string(13) "johndoe" ["display_name"]=> string(13) "John Doe" ["user_email"]=> string(23) "contact@site.com" ["user_url"]=> string(23) "https://siteDOTcom" ["user_registered"]=> string(19) "2019-03-12 03:53:10" ["user_description"]=> string(0) "" ["user_avatar"]=> string(472) "John Doe" } }

Of course I changed some data before posting here, because it's personal data.

I believe that this tutorial https://www.advancedcustomfields.com/resources/querying-relationship-fields/ has something to do with what I'm trying to achieve, but I'm not sure.

Basically it's this:

Field name --
             => Array value 1
             => Array value 2
             => Array value 3

First I need to get the field, then retrieve its "sub" values and display them. I tried to do my best to explain this, if anyone else needs more info, just ask.

Thanks in advance.

PickleRiiiick
  • 31
  • 1
  • 4

2 Answers2

1

To access elements from a 2d array you can do {{ author.user_firstname }} or if there were further level you can do {{ author.level1.level2 }}

FYI, you can still use php and then add the data to your twig files by extending the context.

For example in your single.php

// usual php stuff
$logic = some_logic_function();

// get context
$context = Timber::context();

// add to context
$context['logic'] = $logic;

// render view
Timber::render('single.twig', $context);

Then in your single.twig file you can access the data as such:

{{ logic }}
WPhil
  • 1,056
  • 1
  • 7
  • 12