【问题标题】:How do you call a custom field in a twig template in Wordpress?如何在 Wordpress 的树枝模板中调用自定义字段?
【发布时间】:2015-03-04 02:49:32
【问题描述】:

我有一个使用 twig 模板的 wordpress 网站。我正在尝试调用并显示自定义字段。

我尝试使用几个代码来使其工作。但是失败了。有人可以帮忙吗?

谢谢!

【问题讨论】:

  • 你试过get_post_meta('post_id','custom_field', true);吗?
  • 我的文件是 .twig,所以 php 代码不起作用。不过谢谢!

标签: wordpress wordpress-theming twig custom-fields


【解决方案1】:

您可以使用 post.get_field() 轻松调用 wpcf 值。

例如:

{{post.get_field('wpcf-ticket-url')}}

您也可以创建任何辅助函数来扩展您的 twig 库。您可以将它们分配到您的 twig 主题文件夹中的 functions.php 文件中。

例如:

树枝主题/functions.php:

$context['get_custom_meta'] = TimberHelper::function_wrapper( 'get_custom_meta', array('',0) );//array contains default values

function get_custom_meta($key,$id){
    if(empty($id)){
       return types_render_field($key,  array("raw"=>"true","separator"=>";"));
    }else{
       return get_post_meta( $id, 'wpcf-'.$key, true );
    }
}

twig-theme/views/xxx.twig:

{{ get_custom_meta("ticket-url",item.object_id) }}

【讨论】:

    猜你喜欢
    • 2018-07-03
    • 1970-01-01
    • 1970-01-01
    • 2013-08-07
    • 2016-06-09
    • 1970-01-01
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    相关资源
    最近更新 更多