2017-08-14 149 views
0

我有树枝/木材功能,代码没有显示任何错误,但是当我在我的树枝模板中使用功能时出现错误,说'不是函数。无法识别自定义树枝功能

这是我在树枝模板我fucntions.php文件:

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); 
    } 
} 

这就是我如何调用模板中的函数。

{{ function(get_custom_meta('facebook', event.post_id)) }} 

This in in wordpress。由于

+0

get_custom_meta返回什么?我相信回报是空的。 –

回答

1
{{ get_custom_meta('facebook', event.post_id) }} 

我觉得你像这样访问功能,而不是通过函数()

/** 
* 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')); 
}); 

当您在嫩枝能够使用的功能,通过使用木材\ Twig_Function木材/树枝钩内像这样的你用它像

{# 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 #} 

函数()是用于wordpress的功能

参考: timber twig functions

+0

无论哪种方式,该功能无法识别 –

+0

如果您有许多功能需要使用,并且希望提高代码的可读性,则可以使用木材/枝条钩子内的Timber \ Twig_Function在Twig中创建一个可用的功能。 你做到了吗? https://timber.github.io/docs/guides/functions/ – Cezary

+0

我刚刚尝试过,并没有什么区别。 –