Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Wordpress中调用细枝模板中的自定义字段?_Wordpress_Wordpress Theming_Twig_Custom Fields - Fatal编程技术网

如何在Wordpress中调用细枝模板中的自定义字段?

如何在Wordpress中调用细枝模板中的自定义字段?,wordpress,wordpress-theming,twig,custom-fields,Wordpress,Wordpress Theming,Twig,Custom Fields,我有一个wordpress网站使用了一个小树枝模板。我正在尝试调用并显示自定义字段 我试着用几个代码使它工作。但失败了。有人能帮忙吗 谢谢 您可以使用post.get_field()轻松调用wpcf值 例: 您还可以创建任何帮助器函数来扩展您的细枝库。您可以在您的细枝主题文件夹的functions.php文件中分配它们 例: twig主题/functions.php: $context['get_custom_meta'] = TimberHelper::function_wrapper( 'g

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

我试着用几个代码使它工作。但失败了。有人能帮忙吗


谢谢

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

例:

您还可以创建任何帮助器函数来扩展您的细枝库。您可以在您的细枝主题文件夹的functions.php文件中分配它们

例:

twig主题/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 );
    }
}
{{ get_custom_meta("ticket-url",item.object_id) }}
twig主题/视图/xxx.twig:

$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 );
    }
}
{{ get_custom_meta("ticket-url",item.object_id) }}

您是否尝试过
get_post_meta('post_id','custom_field',true)?我的文件是.twig,因此php代码无法工作。谢谢你!