Why parent menu would output twic in menus range function?

Hi guys:

There I wanted to add children menus in my blog, and then I added a sample children menu configure in config.yaml as below:

menus:
  main:
    - identifier: home
      name: 首页
      pageref: /
      pre: home
      weight: 1
    - identifier: about
      name: 关于
      pageref: /about.html
      pre: user
      weight: 2
    - identifier: flinks
      name: 站点示例
      pageref: /flinks.html
      pre: thumbs-up
      weight: 3
    - identifier: menus
      name: 二级菜单
      #pre: thumbs-up
      weight: 4
    - identifier: menus-children1
      name: 子菜单1
      pageref: '#'
      #pre: thumbs-up
      weight: 1
      parent: 二级菜单
    - identifier: menus-children2
      name: 子菜单2
      pageref: '#'
      #pre: thumbs-up
      weight: 2    
      parent: 二级菜单
    - identifier: archives
      name: 归档
      pageref: /archives
      pre: archive
      weight: 5
    - identifier: commonweal
      name: 公益 404
      url: /404.html
      pre: heartbeat
      weight: 6

as the menus.html were design like this:

<nav class="site-nav">
  <ul class="main-menu menu">
    {{ printf "%s" .Site.Menus.main }}
    {{ range .Site.Menus.main }}
  
    <li>
      <a href="{{ .URL }}">{{ .Name }}</a>
      {{/* 判断一级菜单是否有子菜单 */}}
      {{ if .HasChildren }}
        <ul>
          {{/* 遍历二级菜单 */}}
          {{ range .Children }}
            <li><a href="{{ .URL }}">{{ .Name }}</a></li>
          {{ end }}
        </ul>
      {{ end }}
    </li>
  {{ end }}
  </ul>
</nav>

But there were some strange phenomena in the result. The secondary menu of that example was displayed twice, and it did not appear where I wanted it to appear, as indicated in the red box in the following figure.

Then I printed out .Site.Menus.main and found that it did generate information about the secondary menu twice. What is the reason for this? I tried debugging several times but it was not successful. Please help me take a look. Thank you.

[%!s(*navigation.MenuEntry=&{{home 首页 home / 1 Hugo NexT map[]} main 0xc0030052f0 []}) %!s(*navigation.MenuEntry=&{{about 关于 user /about.html 2 关于 Hugo NexT 组织 map[]} main 0xc0030059e0 []}) %!s(*navigation.MenuEntry=&{{flinks 站点示例 thumbs-up /flinks.html 3 站点示例 map[]} main 0xc003005c80 []}) %!s(*navigation.MenuEntry=&{{menus 二级菜单 4 map[]} main <nil> []}) %!s(*navigation.MenuEntry=&{{archives 归档 archive /archives 5 map[]} main 0xc0030054d0 []}) %!s(*navigation.MenuEntry=&{{commonweal 公益 404 heartbeat /404.html 6 map[]} main /404.html <nil> []}) %!s(*navigation.MenuEntry=&{{ 二级菜单 0 map[]} <nil> [0xc001c56620 0xc001c56700]})]

I think you’re misunderstanding the parent, the parent should be equal to the identifier of parent menu. For example.

    - identifier: menus
      name: 二级菜单

    - identifier: menus-children2
      name: 子菜单2
      pageref: '#'
      #pre: thumbs-up
      weight: 2    
      parent: menus # should be menus, not "二级菜单"
1 Like

You are right. This is my mistake and I referred to an incorrect case where the identifier parameter was not defined. However, in my configuration file, the identifier was indeed defined, so it does not reference name as the parent menu entry.

Thank you!!!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.