0
votes

I have created an ACF field group that is only available on the User > Add/Edit form in the User Profile. An image upload field has been created to handle user profile images.

The code we are using to display the author name and description are {{ post.author.name }} and {{ post.author.description }}. ACF fields can be accessed using the same object-based way if they are attached to the post, but I cannot figure out how to access them when they are attached to the user. Is there something specific that I need to add to the functions.php file in order for them to be part of $context and become accessible in the twig files.

2
can you please add you code to snippet so we can easily understand - Dhruv

2 Answers

0
votes

As per the timber documentation, you can get the ACF fields using the meta.

Set image field return type as image_id. Then you can get the image as below:

$context['current_user'] = new Timber\User();
Timber::render('single.twig', $context);

<img src="{{ Image(current_user.meta('your_field_name')).src }}" />

To get the post author meta field:

<img src="{{ Image(post.author.meta('your_field_name')).src }}" />

0
votes

I have solved my problem with assistance from Jainil. My solution is below.

single.php

$context         = Timber::context();
$timber_post     = Timber::query_post();
$context['post'] = $timber_post;

if ( post_password_required( $timber_post->ID ) ) {
    Timber::render( 'single-password.twig', $context );
} else {
      Timber::render( array( 'single-' . $timber_post->ID . '.twig', 'single-' . $timber_post->post_type . '.twig', 'single-' . $timber_post->slug . '.twig', 'single.twig' ), $context );
}

No additional code is required here. Jainil suggested $context['current_user'] = new Timber\User();, but it is not required for this to work.

single.twig

<img src="{{ Image(post.author.meta('author_profile_image')).src }}" class="" alt="{{ Image(post.author.meta('author_profile_image')).alt }}" />

I hope this helps others.