2017-08-14 148 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

我剛剛嘗試過,並沒有什麼區別。 –