Php 如何编写wordpress函数;是“首页()”;细枝木材编码?

Php 如何编写wordpress函数;是“首页()”;细枝木材编码?,php,wordpress,twig,Php,Wordpress,Twig,我的目标是只在wordpress的索引页上使用细枝代码输出一些东西。我已经设置了一个名为Home的静态页面 我已经在我的基地试过了。小枝: {% if is_front_page %} Homepage content {% endif %} 但这没有任何作用,我只是觉得由于某些原因,我很难找到它 感谢您的帮助!提前感谢Timber(还有函数的别名)让您执行外部PHP函数。因此,类似这样的方法会起作用: {% if fn('is_front_page') %} Homepage cont

我的目标是只在wordpress的索引页上使用细枝代码输出一些东西。我已经设置了一个名为Home的静态页面

我已经在我的基地试过了。小枝:

{% if is_front_page %}
 Homepage content
{% endif %}
但这没有任何作用,我只是觉得由于某些原因,我很难找到它

感谢您的帮助!提前感谢Timber(还有
函数的别名)让您执行外部PHP函数。因此,类似这样的方法会起作用:

{% if fn('is_front_page') %}
  Homepage content
{% endif %}

我喜欢在我的小树枝模板之外保留特殊功能。在Timber中,您可以定义自己的上下文,在其中可以设置自己的变量

创建名为
front page.php的文件,并添加:

<?php

$context = Timber::get_context();

// Set a home page variable
$context['is_front_page'] = 'true';

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

可以通过扩展timber\u上下文fitler来创建全局内容

将以下内容添加到functions.php文件中,它将使用调用Timber::get_context()添加到所有模板中


很好,我已经找到了解决方案,但这是另一种方法!谢谢你让我知道这个功能!这一点在我考虑之后几乎太明显了……这可能就是为什么它工作得这么好的原因。
{% if is_front_page %}
 Homepage content
{% endif %}
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;
}