At W3 Relations, we build eCommerce websites that convert. We help businesses grow through custom design, robust development, and smart digital marketing strategies.
<?php
$menu_name = 'primary';
$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' ) );
$count = 0;
$submenu = false;
foreach( $menuitems as $item ):
// set up title and url
$title = $item->title;
$link = $item->url;
// 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="nav-item">
<a class="nav-link text-white" href="<?php echo $link; ?>"><?php echo $title; ?></a> <?php endif; ?>
<?php if ( $parent_id == $item->menu_item_parent ): ?>
<?php if ( !$submenu ): $submenu = true; ?>
<ul class="navbar-nav ml-auto sub-menu">
<?php endif; ?>
<li class="nav-item">
<a href="<?php echo $link; ?>" class="nav-link text-white"><?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; ?>
Tags:
Author
Md Shukur Miah
I help eCommerce entrepreneurs who are struggling to build high-converting websites. Many business owners face challenges with technical complexities, poor website performance, and low conversions, which hold them back from scaling their online businesses.