Issue when adding second custom menu to global context - Unsupported operand types in /app/wp-content/plugins/timber-library/vendor/twig/twig/lib/Twig/Environment.php(462)
我正在努力抓取第二个注册菜单并将其添加到 Headers 模板中 . 我正在使用带有Twig模板的Timber插件 .
我将第二个菜单添加到全局上下文中:
$context['custom-menu'] = new Timber\Menu( 'Custom Menu' );
并在我的index.php文件中调用菜单 .
$context = Timber::get_context();
$context['posts'] = new Timber\PostQuery();
$templates = array( 'index.twig' );
if ( is_home() ) {
array_unshift( $templates, 'home.twig' );
}
Timber::render( $templates, $context );
header.twig文件包含在base.twig中,该文件扩展为index.twig . 这包括所有 Headers 片段 .
{% include "partial/user-menu.twig" with {'custom-menu': custom-menu.get_items} %}
{% include "menu.twig" with {'menu': menu.get_items} %}
{% include "partial/sl_logo.twig" %}
{% include "partial/sl_search.twig" %}
menu.twig渲染得很好,但是当我添加用户菜单时,它会在顶部给出错误 . 当我删除 with {'custom-menu': custom-menu.get_items}
时,它给了我错误: Object of class Timber\Menu could not be converted to int
user-menu.twig文件如下所示:
<ul>
{% for item in custom-menu %}
<li class="{{ item.classes | join(' ') }}">
<a target="{{ item.target }}" href="{{ item.link }}">{{ item.title }}</a>
{% include "user-menu.twig" with {'custom-menu': item.get_children} %}
</li>
{% endfor %}
</ul>
Timber Github repo上的文档建议您可以将菜单添加到全局上下文中,并在get_context时添加它们 .
https://github.com/timber/timber/blob/master/docs/guides/menus.md
任何人都可以帮助诊断问题吗?
1 回答
也许我会首先向你提供我在相同情况下使用的东西 .
在
functions.php
添加接下来在所需的地方使用: