有人知道如何将自定义字段(来自高级自定义字段插件)插入到细枝模板中吗?
这是我想添加到twig文件中的代码。
<img src="<?php the_field(\'profile_pic\'); ?>" />
这是细枝模板文件的编码
{% extends \'layout - tlt.twig\' %}
{% block content %}
<br> <br>
<h1 style="font-weight:bold; font-size:24px;" class="">{{ post.post_title }}</h1>
<br> <br>
<div class="agent">
<div class="row">
<div class="image span2">
<a href="{{ wp.get_permalink(wp.get_the_ID()) }}">
{% if wp.get_the_post_thumbnail(wp.get_the_ID()) %}
{{ wp.get_the_post_thumbnail(wp.get_the_ID())|raw }}
{% else %}
<img src="{{ wp.get_template_directory_uri() }}/assets/img/agent-tmp.png" alt="{{ property.post_title }}">
{% endif %}
</a>
</div><!-- /.image -->
<div class="body span4">
{{ wp.do_shortcode(wp.apply_filters(\'the_content\', post.post_content))|raw }}
</div><!-- /.body -->
<div class="info span3">
<div style="font-size:14px;" class="box">
<div style="margin-bottom:7px;" class="phone">
<i class="icon icon-normal-mobile-phone"></i>
{{ wp.get_post_meta(wp.get_the_ID(), \'_tlt_mobile\', TRUE) }}
</div><!-- /.phone -->
<div style="margin-bottom:7px;" class="office">
<i class="icon icon-normal-phone"></i>
{{ wp.get_post_meta(wp.get_the_ID(), \'_tlt_phone\', TRUE) }}
</div><!-- /.office -->
<div style="margin-bottom:7px;" class="email">
<i class="icon icon-normal-mail"></i>
<a href="mailto:{{ wp.get_post_meta(wp.get_the_ID(), \'_tlt_email\', TRUE) }}">
{{ wp.get_post_meta(wp.get_the_ID(), \'_tlt_email\', TRUE) }}
</a>
</div><!-- /.email -->
<div style="margin-bottom:7px;" class="location">
<i class="icon icon-normal-phone"></i>
{{ wp.get_post_meta(wp.get_the_ID(), \'_tlt_location\', TRUE) }}
</div><!-- /.office -->
<div style="margin-bottom:7px;" class="location">
<i class="icon icon-normal-phone"></i>
{{ wp.get_post_meta(wp.get_the_ID(), \'contact_number\', TRUE) }}
</div><!-- /.office -->
<div style="margin-bottom:7px;" class="location">
<img src="{{ wp.get_post_meta(wp.get_the_ID(), \'profile_pic\', TRUE)}}" alt="image" />
</div><!-- /.office -->
</div><!-- /.box -->
</div><!-- /.info -->
</div><!-- /.row -->
</div><!-- /.agent -->
{% if properties %}
<hr>
{% endif %}
{% endblock %}
我做了所有正确的事情,直到在中插入代码。细枝文件。什么都没用。
最合适的回答,由SO网友:anu 整理而成
我不使用Twig,但我很确定您必须传递值,而不是模板中的所有WP函数(可能在WordPress的上下文之外运行)。因此,您的控制器(通常是WordPress模板)将把WP调用的结果存储到一个数组中,然后使用存储在数组中的值呈现细枝模板。
[编辑以添加]
您的控制器中有一个呼叫,如下所示:
echo View::render( \'single-tlt.twig\', array(
\'post\' => $post,
\'properties\' => aviators_properties_get_by_agent( get_the_ID() )
)
);
更改为以下内容:
echo View::render( \'single-tlt.twig\', array(
\'post\' => $post,
\'properties\' => aviators_properties_get_by_agent( get_the_ID() ),
\'profile_pic\' => get_the_field( \'profile_pic\' )
)
);
然后在细枝模板中,您可以添加如下内容:
<img src = \'{{profile_pic}}\'>