0
当使用木材库时,是否有选项或方法为所有实例或渲染页面提供数据?查看作曲家或类似的WordPress的木材模板库?
我想核心functions.php
文件中设置一些站点范围内的数据,并将它提供给所有的模板,而不需要每前Timber::render()
当使用木材库时,是否有选项或方法为所有实例或渲染页面提供数据?查看作曲家或类似的WordPress的木材模板库?
我想核心functions.php
文件中设置一些站点范围内的数据,并将它提供给所有的模板,而不需要每前Timber::render()
我会用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);