我正在尝试将我的标准 PHP WordPress 主题转换为 Timber/Twig,并且无法从自定义函数获取任何输出。这个特别查看帖子是否具有 Yoast 主要术语集,它允许您为具有多个类别的帖子指定主要类别。
我需要在 The Loop 中执行此操作,并且大多数文档都在讨论如何在单个页面中执行此操作。我的functions.php中有一个这样的函数:
function my_theme_get_post_category() {
// regular list of categories set in WP
list( $wp_category ) = get_the_category();
// primary category set with Yoast plugin
$primary_category = new WPSEO_Primary_Term( 'category', get_the_ID() );
$primary_category = $primary_category->get_primary_term();
$primary_category = get_category( $primary_category );
// use only one or the other
if ( is_wp_error( $primary_category ) || $primary_category == null ) {
$category = $wp_category;
} else {
$category = $primary_category;
}
return $category;
}
根据我在此处的“功能”部分(https://github.com/timber/timber/wiki/WP-Integration#functions)中读到的内容,我应该可以在我的模板中使用 调用它{{ function('my_theme_get_post_category', post.ID) }}
,但是不起作用。
我尝试制作$postID
函数的必需参数,但这也没有任何帮助。
我也尝试使用TimberHelper::function_wrapper
,然后在模板中调用它,{{ my_theme_get_post_category }}
但同样没有完成任何事情。