skip to Main Content

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) "[email protected]" ["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.

2

Answers


  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 }}
    
    Login or Signup to reply.
  2. as indicated in your comment, acf fields can be accessed in much the same way as you’d access other meta properties.

    if it’s a repeater, it’ll have multiple values, so you can loop over them (only use .meta for the ‘parent’ property)

    code sample for template.twig:

            {% if post.meta('partner_organisations')|length %}
                {% for item in post.meta('partner_organisations') %}{% if item.status == "publish" %}
                    <a href="{{ item.link }}">{{ item.name }}</a><br />{% endif %}
                {% endfor %}
            {% endif %}
    

    here, first we make sure there’s actually some values. as this is for Posts, they have a status which might be draft, so we check for a published status before displaying the linked name. if you wanted an unordered list, you’d add the <ul> tags inside the if

    you can also build an array with the relevant references in your template.php before passing it over to twig.

    other ways of interacting with arrays/lists in timber/twig:

    {{ dump(post.meta('list_field')|first) }}
    {{ post.meta('list_field')|join(', ') }}
    {{ post.meta('list_field')|join(', ', ' and ') }}
    

    documentation on nested repeaters and more is available here https://timber.github.io/docs/guides/acf-cookbook/#nested-repeater-fields

    if you’re looking for details specifically using a relationship field – the doc above also has info on that, as you;d want to make sure it’s a TimberPost so you can interact with its properties as usual:

    {% for item in Post(post.relationship_field) %}
       {{ item.title }}
       {# Do something with item #}
    {% endfor %}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search