php - 在树枝中返回 acf 图像 src
问题描述
我无法让 acf 图像返回。
在我的 single-work.twig (查看文件)中,我有
<img src="{{ Image(post.meta(‘laptop_image')).src }}" />
在我有的 single-work.php (控制器文件)模板中
$context[‘laptop_image'] = get_field(‘laptop_image’);
我也试过
在 single-work.php(控制器文件)中
$post->laptop_image = new Timber\Image($post->laptop_image);
在 single-work.twig 中(查看文件)
<img src="{{ post.laptop_image.src | resize(500, 300) }}" />
大多数时候我得到“未知”的结果..我曾经得到返回的图像 ID,但我不记得我是怎么得到的......任何帮助都会很棒。
完整的 single-work.php
$context = Timber::context();
$timber_post = Timber::query_post();
$context['post'] = $timber_post;
$context['text_with_image_title'] = get_field('headline');
$context['text_with_image_text'] = get_field('description');
$post->laptop_image = new Timber\Image($post->laptop_image);
$context['ipad_image'] = get_field('ipad_image');
$context['iphone_image'] = get_field('iphone_image');
$context['text_with_image_btn_link'] = get_field('work_url');
$context['text_with_image_btn_label'] = 'Go to ' . $timber_post->post_title . ' Website';
$context['text_with_image_btn_target'] = '_blank';
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', '/pages/single-work.twig' ), $context );
}
var_dump($post->laptop_image); //returns image id
work.twig 的完整代码
<div class="col-6 text-with-image__column text-with-image__column--image">
<div class="text-with-image__wrapper">
<img src="{{ post.laptop_image.src | resize(500, 300) }}" />
{{dump(post.laptop_image)}} //returns nothing
</div>
</div>
解决方案
我意识到我正在使用宏,因此
<img src="{{ Image(post.laptop_image).src }}" />
工作正常,尽管它需要作为新实例的宏值应用。不仅仅是模板的一部分。
推荐阅读
- spring-boot - 尝试从 docker 上发布的 Neo4jRepository 检索数据时出现 Neo4jSession 错误[ONLY DOCKER ISSUE]
- ios - 将 UIView/UIGestureRecognizer 添加到 UIPresentationController 中的呈现视图
- tensorflow - 如何在云中部署实时学习张量流模型?
- apache-spark - 提取键值时保持从 MapType 中键的顺序
- jenkins-pipeline - 从 shell 阶段中止 jenkins pipline 项目
- java - 登录后如何为不同角色的用户(如学生、教师和家长)添加导航抽屉?
- angular - 角度用 formArray 填充数据
- python - 寻求澄清 math.isclose()
- npm - npm install package global 并在一个 cmd 中链接
- c# - 删除一些带有特殊字符的特定字符串