Support

Account

Home Forums General Issues How can I use Advanced Custom Fields with timber to retrieve Array values?

Solving

How can I use Advanced Custom Fields with timber to retrieve Array values?

  • I’m trying to retrieve the user’s metadata by first calling a custom field, 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, 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’: ‘</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&#8221; [“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.

  • Sorry, I’m probably not going to be of much help to you. But you will find that integration of ACF into page builders and template engines is scarce and getting help here will be hard unless someone that has experience with both happens along.

    However, I can give it a stab, you say that you are using Timber. The file name ends in .twig, so I am assuming that timber uses twig templates. If this is the case then there is an example of looping over an array there.

    Again, this is only a guess using the example taken from that document.

    
    {% for item in author %}
      <li><a href="{{ item.user_url }}">{{ item.display_name }}</a></li>
    {% endfor %}
    
  • I’m totally new to timber, someone just told me the language is called twig, I’m confused.

  • I tried using:

    {% for item in post_autor %}
      <li><a href="{{ item.user_url }}">{{ item.display_name }}</a></li>
    {% endfor %}

    for the custom field, but it’s not working.

    I also tried:

    {% for item in author %}
      <li><a href="{{ item.user_url }}">{{ item.display_name }}</a></li>
    {% endfor %}

    Same result.

    On the other hand, when I tried:

    <p class="byline">Autor: <a href="{{ post.get_field("post_autor.user_url") }}">{{ post.get_field("post_autor") }}</a></p>

    it worked after I settled the type to User Object, but the URL being retrieved is the post’s url instead of the user profile url.
    Since there’s too many users being listed as contributors, using “author” won’t work for all of them (I presume).

  • Going to be honest, I don’t really have a clue. I am completely unfamiliar with the syntax required. The odds of finding someone to help you here are slim. You need someone that does custom work using Timber/Twig. You might want to try posting your question on Stack where you’ll get a wider audience.

  • Alright, I have an idea: how should I proceed if I was create a structure like this, using ACF and PHP:

    1 – Set custom field as User Array or User Object.
    2 – Display the following structure:

    | User Avatar | User’s Name (linked to their profile).

    I need it to display the same way that the avatar and name are displayed here in this support forum. If I know how proceed with php, I might be able to convert it to twig later.

  • 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 …

  • What is your problem?

  • The post ID where the value is saved. Defaults to the … (array) This function will return an array looking something like

  • I have same question for loop images with ACF

    I tried like this but nothing. Galeria slider is the slug name.

    What we must put for img src loop or syntax to check images in this gallery ACF¿?

    <div class="slideshow">
    {% for image in post.get_field('galeria_ slider') %}
        <img src="{{cover_image.src}}" />
    {% endfor %}</div>

    I must declare something like this ?

    $context['cover_image'] = Timber::post.get_field('galeria_ slider');

    Yes or not ? and if yes, where exactly. (function.php and into function __construct())

    many thanks

  • I try also but nothing :

    {% for image in post.meta('gallery') %}
        <img src="{{ Image(image) }}" />
    {% endfor %}
Viewing 12 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic.