在树枝中返回acf图像src

时间:2020-07-16 05:14:02

标签: php wordpress twig advanced-custom-fields

我无法获得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

完整的工作代码。小枝

<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>

1 个答案:

答案 0 :(得分:0)

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

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