Skip to content

Instantly share code, notes, and snippets.

@rajucs
Created November 19, 2021 02:14
Show Gist options
  • Save rajucs/a03eadf7a865d0654cd7d8816b093bb2 to your computer and use it in GitHub Desktop.
Save rajucs/a03eadf7a865d0654cd7d8816b093bb2 to your computer and use it in GitHub Desktop.
<?php
$menu_name = 'main-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' ) );
?>
<ul class="nav">
<?php
$count = 0;
$submenu = true;
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>
<a href="<?php echo $link; ?>">
<span><?php echo $title; ?></span>
</a>
<?php endif; ?>
<?php if ( $parent_id == $item->menu_item_parent ): ?>
<?php if ( !$submenu ): $submenu = true; ?>
<div class="mega-menu sub-menu-columns">
<div class="mega-bg">
<div class="mega-mrg">
<div class="maga-half">
<ul>
<?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>
</div>
</div>
</div>
</div>
<?php $submenu = false; endif; ?>
<?php endif; ?>
<?php if (@$menuitems[ $count + 1 ]->menu_item_parent != $parent_id ): ?>
</li>
<?php $submenu = false; endif; ?>
<?php $count++; endforeach; ?>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment