Php 无法识别自定义细枝函数

Php 无法识别自定义细枝函数,php,wordpress,twig,timber,Php,Wordpress,Twig,Timber,我有一个twig/timber函数,代码没有显示任何错误,但当我在twig模板中使用函数时,出现一个错误,表示“”不是函数 这是我在twig模板中的functions.php文件中的内容: add_filter( 'timber/twig', function( \Twig_Environment $twig ) { $twig->addFunction( new Twig_Function( 'get_custom_meta', 'get_custom_meta' ) ); }

我有一个twig/timber函数,代码没有显示任何错误,但当我在twig模板中使用函数时,出现一个错误,表示“”不是函数

这是我在twig模板中的functions.php文件中的内容:

add_filter( 'timber/twig', function( \Twig_Environment $twig ) {
    $twig->addFunction( new Twig_Function( 'get_custom_meta', 'get_custom_meta' ) );
})

这就是我在模板中调用函数的方式

{{ function(get_custom_meta('facebook', event.post_id )) }}
这是wordpress中的。谢谢

{{ get_custom_meta('facebook', event.post_id ) }}
我认为您可以像这样访问函数,而不是通过函数()访问函数

当您在Timber/Twig挂钩内使用Timber\Twig_函数使函数在Twig中可用时,您可以这样使用它

{# 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 #}
{#single.twig}
{{edit_post_link}
{#使用默认参数调用edit#u post_链接}
{#single-my-post-type.twig}
{{edit_post_link(null,,)}
{#使用所有默认值调用edit#u post_链接,第二个参数除外}
函数()用于wordpress函数

参考:

什么是“自定义”元返回?我相信返回值是空的。无论哪种方式,函数都无法识别。如果您有大量使用的函数,并且希望提高代码的可读性,则可以在Timber/Twig钩子中使用Timber\Twig_函数,使函数在Twig中可用。你做到了吗?我是否应该将函数添加到特定视图中?@MattStacey可能尝试阅读函数包装器,这可能有助于我认为解决方案是正确的@MattStacey,您可以使用
{{get_custom_meta('facebook',event.post_id)}
而不是
{function(get_custom_meta('facebook',event.post_id))}
或者使用
{function('get_custom u meta,'facebook',event.post_id))}调用函数。澄清一下:自从Timber1.3.0以来,
函数\u wrapper()
函数就被弃用了。最新指南可在官方文件的第页找到。wiki中的条目也已弃用,并将很快被删除。
/**
 * 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' ) );
} );
{# 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 #}