首页 > 解决方案 > 在树枝中返回 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>

标签: phpwordpresstwigadvanced-custom-fields

解决方案


我意识到我正在使用宏,因此

<img src="{‌{ Image(post.laptop_image).src }}" />工作正常,尽管它需要作为新实例的宏值应用。不仅仅是模板的一部分。


推荐阅读