我的目标是只在wordpress的索引页上使用细枝代码输出一些东西。我已经设置了一个名为Home的静态页面。
我已经在我的基地试过了。细枝:
{% if is_front_page %}
Homepage content
{% endif %}
但这并没有什么作用,我只是发现由于某种原因不能轻易找到它。
任何帮助都很感激!提前感谢
Timber带有fn
(也有函数
的别名),可以让您执行外部PHP函数。所以像这样的东西会起作用:
{% if fn('is_front_page') %}
Homepage content
{% endif %}
我喜欢在我的小树枝模板之外保留特殊功能。在Timber中,您可以定义自己的上下文,在其中可以设置自己的变量。
创建一个名为首页的文件。php
并添加:
<?php
$context = Timber::get_context();
// Set a home page variable
$context['is_front_page'] = 'true';
Timber::render(array('home.twig'), $context);
然后,您可以使用is_front_page
作为变量,就像您想要的那样:
{% if is_front_page %}
Homepage content
{% endif %}
可以通过扩展timber\u上下文fitler来创建全局内容。
将以下内容添加到您functions.php文件中,它将使用调用Timber::get_context();添加到所有模板中。
add_filter('timber_context', 'add_to_context');
function add_to_context($context){
/* Add to Timber's global context */
$context['is_front_page'] = is_front_page();
return $context;
}