如何使用“wp_get_nav_menu_items”在WordPress中显示3级分层菜单?

问题描述 投票:0回答:1

我试图使用wp_get_nav_menu_items函数在WordPress中显示3级分层菜单,但不知何故,结构没有以这种方式显示。

这是我在头文件中包含的代码,但结构不正确。

$menu_name = 'primary-menu'; 
     $locations = get_nav_menu_locations(); 
     $menu = wp_get_nav_menu_object($locations[$menu_name]); 
     $menuitems = wp_get_nav_menu_items($menu->term_id, array('order' => 'DESC')); 
     $ParentArray = array(); 
     foreach ($menuitems as $item) { 
       // if (!empty($item->menu_item_parent) && !in_array($item->menu_item_parent, $ParentArray)) { 
             array_push($ParentArray, $item->ID); 
        // } 
     }
      ?>

<nav>
  <ul class="main-nav">
    <?php 
        $count = 0; 
        $submenu = false; 
        foreach ($menuitems as $item): 
            $link = $item->url; 
            $title = $item->title; 
            // item does not have a parent so menu_item_parent equals 0 (false) 
            if (!$item->menu_item_parent): 
                // save this id for later comparison with sub-menu items 
                $parent_id = $item->ID; 
                ?>

    <li class="item">
      <a href="
        <?php echo $link; ?>" class="title">
        <?php echo $title; ?>
      </a>
      <?php endif; ?>
      <?php if (in_array($item->menu_item_parent, $ParentArray)): ?>
      <?php if (!$submenu): $submenu = true; ?>
      <ul class="sub-menu">
        <?php endif; ?>
        <li class="item">
          <a href="
            <?php echo $link; ?>" class="title">
            <?php echo $title; ?>
          </a>
        </li>
        <?php if (!isset($menuitems[$count + 1]) || $menuitems[$count + 1]->menu_item_parent != $parent_id && $submenu): ?>
      </ul>
      <?php 
          $submenu = false; 
          endif; 
          ?>
      <?php endif; ?>
      <?php if (!isset($menuitems[$count + 1]) || $menuitems[$count + 1]->menu_item_parent != $parent_id): ?>
    </li>
    <?php 
        $submenu = false; 
        ?>
    <?php 
        $count++; 
        endforeach; 
        ?>
  </ul>
</nav>
ajax wordpress menu wordpress-theming nav
1个回答
1
投票

在对上面的代码进行更改之后,这件事对我有用。

    $menu_name = 'primary-menu'; 
     $locations = get_nav_menu_locations(); 
     $menu = wp_get_nav_menu_object($locations[$menu_name]); 
     $menuitems = wp_get_nav_menu_items($menu->term_id, array('order' => 'DESC')); 
     $ParentArray = array(); 
     foreach ($menuitems as $item) { 
       // if (!empty($item->menu_item_parent) && !in_array($item->menu_item_parent, $ParentArray)) { 
             array_push($ParentArray, $item->ID); 
        // } 
     }?>
<nav>
  <ul class="main-nav">
    <?php 
        $count = 0; 
        $submenu = false; 
        foreach ($menuitems as $item): 
            $link = $item->url; 
            $title = $item->title; 
            // item does not have a parent so menu_item_parent equals 0 (false) 
            if (!$item->menu_item_parent): 
                // save this id for later comparison with sub-menu items 
                $parent_id = $item->ID;  ?>

    <li class="item">
      <a href="
        <?php echo $link; ?>" class="title">
        <?php echo $title; ?>
      </a>
      <?php endif; ?>
      <?php if (in_array($item->menu_item_parent, $ParentArray)): ?>
      <?php if (!$submenu): $submenu = true; ?>
      <ul class="sub-menu">
        <?php endif; ?>
        <li class="item">
          <a href="
            <?php echo $link; ?>" class="title">
            <?php echo $title; ?>
          </a>
        </li>
        <?php if (!isset($item[$count + 1]) || $item[$count + 1]->menu_item_parent != $parent_id && $submenu): ?>
      </ul>
      <?php 
          $submenu = false; 
          endif; 
          ?>
      <?php endif; ?>
      <?php if (!isset($menuitems[$count + 1]) || $menuitems[$count + 1]->menu_item_parent != $parent_id): ?>
    </li>
    <?php 
        $submenu = false; 
        ?>
    <?php 
        $count++; 
        endforeach; 
        ?>
  </ul>
</nav>
© www.soinside.com 2019 - 2024. All rights reserved.