以木材代码显示用户的高级自定义字段字段

我创建了一个ACF字段组,该组仅在用户配置文件中的“用户”>“添加/编辑”表单上可用。已创建一个图像上传字段来处理用户个人资料图像。

我们用于显示作者姓名和描述的代码为{{ post.author.name }}{{ post.author.description }}。如果将ACF字段附加到帖子,则可以使用相同的基于对象的方式对其进行访问,但是我无法弄清楚将它们附加到用户后如何访问它们。为了使它们成为$context的一部分并可以在细枝文件中访问,我是否需要添加一些特定的内容到functions.php文件中。

huangyu0047 回答:以木材代码显示用户的高级自定义字段字段

根据木材documentation,您可以使用meta获取ACF字段。

将图像字段返回类型设置为image_id。然后您可以得到如下图像:

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

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

要获取帖子作者元字段:

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

,

我在贾尼尔(Jainil)的帮助下解决了我的问题。我的解决方案如下。

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 );
}

此处不需要其他代码。 Jainil建议使用$context['current_user'] = new Timber\User();,但这不是必需的。

single.twig

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

我希望这对其他人有帮助。

本文链接:https://www.f2er.com/3105453.html

大家都在问