Last active — forked from anonymous/gist:4344870

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Generate a custom structure for Wordpress menus.

View gist:4345363
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
<?php
$menu_name = 'main_nav';
$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' ) );
?>
 
<nav>
<ul class="main-nav">
<?php
$count = 0;
$submenu = false;
foreach( $menuitems as $item ):
// get page id from using menu item object id
$id = get_post_meta( $item->ID, '_menu_item_object_id', true );
// set up a page object to retrieve page data
$page = get_page( $id );
$link = get_page_link( $id );
// 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 $page->post_title; ?>
</a>
<a href="<?php echo $link; ?>" class="desc">
<?php echo $page->post_excerpt; ?>
</a>
<?php endif; ?>
 
<?php if ( $parent_id == $item->menu_item_parent ): ?>
 
<?php if ( !$submenu ): $submenu = true; ?>
<ul class="sub-menu">
<?php endif; ?>
 
<li class="item">
<a href="<?php echo $link; ?>" class="title"><?php echo $page->post_title; ?></a>
<a href="<?php echo $link; ?>" class="desc"><?php echo $page->post_excerpt; ?></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; ?>
 
</ul>
</nav>

Hello.
Add a description menu ?

Hi, before all, thanks for share your solution! Very apreciated!
Well, how can I add more 2 deep levels in sub menu? I tryed to copy and paste the fragment of 'ul.sub-menu' inside of the child 'li' but all is duplicated... =(

How can I do that? Please help me! Thanks!

@theodorocaliari This code should drill down to any level sub menus.

Hi. Your code is buggy. If you add a category to menu a error is shown. $page->post_title change with $item->title and $link = get_page_link( $id ) with $link = $item->url;. Also $page->post_excerpt won't exist.

SpartakusMd is right. I have to add that you must also have an 'isset' on both '($menu_items[$count + 1]->menu_item_parent)' pieces of code, or you'll get a property of non-object error. Also on the first 'isset' since you're closing the '' tag, make sure you use an else statement to close the tag even if it isn't set, or else you'll be missing a closing tag. I also tested it to multiple levels but it doesn't go that far.

I've run into a problem this code, it works great until you need to add a 3rd level menu-item, at which point the html elements come undone and the child menu items don't show up. I've checked the code to see what could be causing this or what I could do to fix it but to no avail. I've posted a questions here:

http://wordpress.stackexchange.com/questions/173573/wp-custom-nav-menu-problem

If you have any advice/help I'd greatly appreciate it.

its worked with me by change

$page = get_page( $id );
$link = get_page_link( $id );
to
$title = $item->title;
$url = $item->url;
$class = $item->classes[0];

good work you saved my day! Thank you.

@mashro3na can you elaborate? you can't just replace two variables. @hitautodestruct can you fix this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.