Check if menu_item has children or has parent

I am using wp_get_nav_menu_items() to get the menu items and this is my HTML structure.

Only items with submenu items should have the dropdown class attached to the li tag.

<ul class="nav navbar-nav">

  <li>
    <a href="http://localhost/technia/" class="title">
      <i class="fa fa-home"></i>                                
    </a>
  </li>

  <li class="dropdown">
    <a href="http://localhost/technia/category/science/" class="title">
      Science                                
    </a>
    <ul class="dropdown-menu">
      <li>
        <a href="http://google.com" class="title">Google</a>
      </li>
      <li>
        <a href="http://yahoo.com" class="title">Yahoo</a>
      </li>
    </ul>
  </li>

  <li class="dropdown">
    <a href="http://localhost/technia/category/vr/" class="title">
      VR                                
    </a>
    <ul class="dropdown-menu">
      <li>
        <a href="http://reddit" class="title">Reddit</a>
      </li>
    </ul>
  </li>

  <li>
    <a href="http://localhost/technia/category/physics/" class="title">
      Physics                                
    </a>
  </li>

  <li>
    <a href="http://localhost/technia/category/medicine/" class="title">
      Medicine                                
    </a>
  </li>

  <li>
    <a href="http://localhost/technia/category/tech/" class="title">
      Tech                                
    </a>
  </li>

  <li>
    <a href="http://localhost/technia/videos/" class="title">
      Videos                                
    </a>
  </li>

</ul>

Currently with my code, I am able to retrieve all menus and their children and display them correctly, however I am adding the class dropdown to ALL list-items whether they have submenus or not which not what I want.

I want to check if the menu has children and if yes, then attach the dropdown class to the li class.

<ul class="nav navbar-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;
$parent_name = $item->title;
$parent_url = $item->url;
?>
  // this is where I need to check if the item has children so I can add or remove the 'dropdown' class, at the moment it is being added to all menu items.
  <li class="dropdown">

    <a href="<?php echo $link; ?>" class="title">
      <?php echo $title; ?>
    </a>

    <?php endif; ?>

    <?php if ( $parent_id == $item->menu_item_parent ): ?>

    <?php if ( !$submenu ): $submenu = true; ?>

    <ul class="dropdown-menu">
      <?php endif; ?>

      <li>
        <a href="<?php echo $link; ?>" class="title"><?php echo $title; ?></a>
      </li>

      <?php if ( $menuitems[ $count + 1 ]->menu_item_parent != $parent_id && $submenu ): ?>
    </ul>
    <?php $submenu = false; endif; ?>

    <?php endif; ?>

    <?php if ( $menuitems[ $count + 1 ]->menu_item_parent != $parent_id ): ?>
  </li>
  <?php $submenu = false; endif; ?>

  <?php $count++; endforeach; ?>

Read more here: Check if menu_item has children or has parent

Leave a Reply

Your email address will not be published. Required fields are marked *