简体   繁体   English

3级深木菜单(WordPress)

[英]3 Level Deep Timber menu (Wordpress)

I'm not familiar with timber at all, but am helping a friend finish up a project that was built with it. 我一点都不熟悉木材,但是我正在帮助一个朋友完成用它建造的项目。 So any help would go a long way please! 因此,任何帮助都将大有帮助!

I have only the first two tiers showing up. 我只显示前两层。 Is there a way to call on the child of a child? 有什么方法可以拜访孩子的孩子?

I'm using the code here, and added to it another tier under child https://timber.github.io/docs/guides/menus/ 我在这里使用代码,并将其添加到子https://timber.github.io/docs/guides/menus/下的另一层

{% if menu %}

<div class="header-menu-items">

  <div class="header-menu-item mod-title">
    <a href="{{ site.url }}" class="" rel="home">
      <div class="header-item-logo">
        <div class="sitelogo">{{ site.name }}</div>
      </div>
    </a>
  </div>

  {% for item in menu.get_items() %}
    <div class="header-menu-item {{ item.current ? 'is-active' : '' }}">

      <div class="header-menu-item-link">
        <a target="{{ item.target }}" href="{{ item.link }}">{{ item.title }}</a>
      </div>

      <div class="header-menu-item-triangle"></div>

      <div class="header-menu-item-mega {{ item.section_colour ? " mod-#{item.section_colour}" : '' }}">

        {% if item.master_object.thumbnail %}
          <div class="mega-image mod-image" style="background-image: url( {{item.master_object.thumbnail.src('thumbnail') }} )">
        {% else %}
          <div class="mega-image">
        {% endif %}
          {{ item.title }}
        </div>

        <div class="mega-items">
          {% for child in item.children %}
            <div class="mega-item">
              <a target="{{ child.target }}" href="{{ child.link }}">
                <span class="mega-item-title">{{ child.title }}<br /></span>
                <span class="mega-item-excerpt">Mega menu description lorem ipsum dolores</span>
              </a>
            </div>
            {% for child in child.children %}
               Just testing to see if it'll even show up first before applying style<br />
               {{ child.title }}<br />
            {% endfor %}
          {% endfor %}
        </div>
      </div>


    </div>
  {% endfor %}

</div>
{% endif %}

You can access menus several layers deep by nesting for loops inside of one another. 您可以通过在另一个内部嵌套循环来访问几层菜单。 Here is a code snippet that I have tested and works. 这是我已测试并可以正常工作的代码段。

{% for item in menu__main.items %} {# This is the top level #}
  <p>{{ item }}</p>

  {% if item.children %}
    {% for child in item.children %} {# 2nd Level #}
      <p><em>{{ child }}</em></p>

      {% if child.children %}
        {% for third in child.children %} {# 3rd Level #}
          <p><strong>{{ third }}</strong></p>
        {% endfor %} {# for third in child.children #}
      {% endif %} {# if child.children #}

    {% endfor %} {# for child in item.children #}
  {% endif %} {# if item.children #}
{% endfor %} {# for item in menu__main.items #}

I have added comments to the end of the lines to hopefully make this more clear. 我已经在各行的末尾添加了注释,希望可以使这一点更加清楚。 So at the top, you are looping through item in menu__main.items 因此,在顶部,您正在遍历item in menu__main.items

Then to get the children inside of these, you loop through item.children since item is the variable that represents each nav item at the top/main level. 然后, item.children这些子项成为其中的子项,就可以遍历item.children因为item是代表顶级/主项上每个导航项的变量。 You loop through item.children to get to the next level or the children inside of the main/top level. 您可以遍历item.children以进入下一个级别或进入主/顶层级别的子级。

Then to get inside of the third level, you loop through child.children since child is the variable that represents the 2nd level. 然后要进入第三级,您可以遍历child.children因为child是代表第二级的变量。 We want to loop through the children of this 2nd level. 我们想遍历第二级的孩子。 so we do third in child.children . 所以我们third in child.children third is the variable that represents the items 3 levels down. third是代表向下3级项目的变量。

I hope you can see the pattern here and you can continue this even further if you have items at even deeper levels, although at some point it will probably get ridiculous. 我希望您能在这里看到这种模式,如果您有更深层次的项目,则可以继续进行下去,尽管有时可能会变得很荒谬。 Like if you have items nested 5 or 6 levels deep. 就像您有嵌套5层或6层深度的物品一样。

Let me know if that makes sense and if not I will be more than happy to try and explain it another way if you still have questions. 让我知道这是否有意义,如果您还有疑问,我将非常乐于尝试以另一种方式进行解释。

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM