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



我创建了一个ACF字段组,该组仅在用户配置文件中的用户>添加/编辑窗体上可用。已经创建了一个图像上传字段来处理用户配置文件图像。

我们用来显示作者姓名和描述的代码是{{ post.author.name }}{{ post.author.description }}。如果ACF字段附加到post,则可以使用相同的基于对象的方式访问它们,但我不知道当它们附加到用户时如何访问它们。是否有一些特定的东西需要添加到functions.php文件中,以便它们成为$context的一部分并在trick文件中可以访问。

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

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

$context['current_user'] = new TimberUser();
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 TimberUser();,但这并不是必须的。

单枝

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

我希望这能帮助其他人。

最新更新