2016-07-14 25 views

回答

1

我會用timber_context過濾器進行手動添加(或timber\context )在使用get_context時添加您自己的數據。

下面是如何增加一個菜單/導航(從Wiki page on TimberMenu)的例子:

add_filter('timber_context', function($context) { 
    /* So here you are adding data to Timber's context object, i.e... */ 
    $context['foo'] = 'I am some other typical value set in your functions.php file, unrelated to the menu'; 

    /* Now, in similar fashion, you add a Timber menu and send it along to the context. */ 
    $context['menu'] = new Timber\Menu(); // This is where you can also send a WordPress menu slug or ID 

    return $context; 
}); 

你必須做的就是最低數據到您的模板將爲:

$context = Timber::get_context(); 

Timber::render('template.twig', $context);