Wordpress 将第二个自定义菜单添加到全局上下文时出现问题

Wordpress 将第二个自定义菜单添加到全局上下文时出现问题,wordpress,twig,wordpress-theming,timber,Wordpress,Twig,Wordpress Theming,Timber,我正在抓取第二个注册菜单并将其添加到标题模板中。我正在使用带有细枝模板的木材插件 我将第二个菜单添加到全局上下文: 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) 调用index.php文件中的菜单 $

我正在抓取第二个注册菜单并将其添加到标题模板中。我正在使用带有细枝模板的木材插件

我将第二个菜单添加到全局上下文:

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)
调用index.php文件中的菜单

$context['custom-menu'] = new Timber\Menu( 'Custom Menu' );
header.twig文件包含在扩展为index.twig的base.twig中。这包括所有的标题部分

$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 );
menu.twig的渲染效果很好,但是当我添加用户菜单时,它会在顶部显示错误。当我用{'custom-menu':custom menu.get_items}删除
时,它给了我一个错误:
Object of class Timber\menu无法转换为int

user-menu.twig文件如下所示:

{% 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" %}
    {%用于自定义菜单中的项%}
  • {%include“user menu.twig”和{'custom-menu':item.get_children}%}
  • {%endfor%}
Timber Github repo上的文档建议您可以将菜单添加到全局上下文中,并在获得上下文时简单地添加它们


有人能帮我诊断这个问题吗?

也许我会先向您提供我在相同情况下使用的

functions.php中
add

    <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>
下次在所需位置使用:

function add_to_context( $data ){
    //menu
    $data['menu_primary'] = new TimberMenu( 'primary' );
    $data['menu_footer'] = new TimberMenu( 'footer' );
    return $data;
}

你的bug修好了吗?我和你有同样的问题。谢谢。我在木材菜单上遇到了麻烦,最后发现它不喜欢名称中包含破折号。我将
custom menu
更改为
custom\u menu
,并开始工作。+1因为这篇文章让我意识到我调用了TimberMenu,使用的是菜单id
Footer
,而不是菜单位置slug
Footer
{% for item in menu_primary.get_items %}
...
{% endfor %}