I’m trying to convert my standard PHP WordPress theme to Timber/Twig and am having trouble getting any output from custom functions. This one in particular looks to see if the post has a Yoast Primary Term set, which allows you to specify a primary category for a post that has multiple categories.

I need to do this in within The Loop and most of the documentation talks about how to do it in a single page. I have a function like this in my 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;

Based on what I’ve read in the “Functions” section here (https://github.com/timber/timber/wiki/WP-Integration#functions), I should be able to call this in my template with {{ function('my_theme_get_post_category', post.ID) }}, but that does not work.

I tried making $postID a required parameter of the function, but that also didn’t help anything.

I also tried using the TimberHelper::function_wrapper and then calling it in the template with {{ my_theme_get_post_category }} but, again, that didn’t accomplish anything.

Read more here: How can I make a WordPress custom function available in a Timber index page?


If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Wordpress related questions and answers: