无法识别自定义树枝功能 [英] Custom Twig Function Not Recognized
问题描述
我有一个 twig/timber 函数,代码没有显示任何错误,但是当我在我的 twig 模板中使用函数时,出现错误提示不是函数".
这是我在树枝模板中的 fucntions.php 文件中的内容:
add_filter( '木材/树枝', 函数( \Twig_Environment $twig ) {$twig->addFunction(new Twig_Function('get_custom_meta', 'get_custom_meta'));
} );
function get_custom_meta($key,$id){如果(空($ id)){return types_render_field($key,array("raw"=>"true","separator"=>";"));}别的{返回 get_post_meta( $id, 'wpcf-'.$key, true );}}
这就是我在模板中调用函数的方式.
{{ function(get_custom_meta('facebook', event.post_id)) }}
这是在 wordpress 中.谢谢
{{ get_custom_meta('facebook', event.post_id ) }}
我认为您可以像这样访问函数而不是通过 function()
/*** 我的自定义 Twig 功能.** @param Twig_Environment $twig* @return $twig*/add_filter( '木材/树枝',函数( \Twig_Environment $twig ) {$twig->addFunction(new Twig_Function('edit_post_link', 'edit_post_link'));});
当你像这样在木材/树枝钩子中使用 Timber\Twig_Function 在 Twig 中提供一个函数时,你可以像那样使用它
{# single.twig #}<div class="admin-tools">{{ edit_post_link }}
{# 使用默认参数调用 edit_post_link #}{# single-my-post-type.twig #}<div class="admin-tools">{{ edit_post_link(null, '<span class="edit-my-post-type-link">') }}
{# 使用所有默认值调用 edit_post_link,除了第二个参数 #}
function() 用于 wordpress 函数
参考:木材树枝功能
I have a twig/timber function and the code doesn't shows any error but when I go to use function in my twig template there is an error saying '' is not a function.
This is what I have in my fucntions.php file in the twig template:
add_filter( 'timber/twig', function( \Twig_Environment $twig ) {
$twig->addFunction( new Twig_Function( 'get_custom_meta', 'get_custom_meta' ) );
} );
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 );
}
}
and this is how I'm calling the function in the template.
{{ function(get_custom_meta('facebook', event.post_id )) }}
This in in wordpress. Thanks
{{ get_custom_meta('facebook', event.post_id ) }}
I think you access functions like this rather than through function()
/**
* My custom Twig functionality.
*
* @param Twig_Environment $twig
* @return $twig
*/
add_filter( 'timber/twig', function( \Twig_Environment $twig ) {
$twig->addFunction( new Twig_Function( 'edit_post_link', 'edit_post_link' ) );
} );
when you make a function available in Twig by using Timber\Twig_Function inside the timber/twig hook like this you use it like that
{# single.twig #}
<div class="admin-tools">
{{ edit_post_link }}
</div>
{# Calls edit_post_link using default arguments #}
{# single-my-post-type.twig #}
<div class="admin-tools">
{{ edit_post_link(null, '<span class="edit-my-post-type-link">') }}
</div>
{# Calls edit_post_link with all defaults, except for second argument #}
the function() is for wordpress functions
refer to: timber twig functions
这篇关于无法识别自定义树枝功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!