WordPress menu, displaying parent child
-
I am having trouble in displaying wp menu. My code works well except for displaying submenu items in correct format.
I am trying to make new row when submenu reaches 4 items in row. For ex:<ul class="rd-navbar-megamenu"> <li> <ul class="rd-megamenu-list"> <li><a>BMW</a></li> </ul> </li> <li> <ul class="rd-megamenu-list"> <li><a>Merwcedes</a></li> </ul> </li> <li> <ul class="rd-megamenu-list"> <li><a>Jetta</a></li> </ul> </li> <li> <ul class="rd-megamenu-list"> <li><a>Porche</a></li> </ul> </li>
// when loop gets here I need to break it with
<ul class=”rd-navbar-megamenu”> and start again. That will make new row, so each row in submenu will have 4 items
<ul class="rd-navbar-nav"> <?php $count = 0; $a = 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> <a href="<?php echo $link; ?>" class="title"> <?php echo $title; ?> </a> <?php endif; ?> <?php if ( $parent_id == $item->menu_item_parent ): $a++; if ( !$submenu ): $submenu = true; ?> <ul class="rd-navbar-megamenu"> <?php endif; ?> <li> <ul class="rd-megamenu-list"> <li><a href="<?php echo $link; ?>" class="title"><?php echo $item->title; ?></a></li> </ul> </li> <?php // problem occurs here, it does not format code well, I dont know if counter is on wrong position. if ( $menuitems[ $count + 1 ]->menu_item_parent != $parent_id && $submenu ): /* close </ul> and start new <ul class="rd-megamenu-list"> */ if($a % 4 == 0){ echo '</ul><ul class="rd-megamenu-list">'; } ?> </ul> <?php $submenu = false; endif; ?> <?php endif; ?> <?php if ( $menuitems[ $count + 1 ]->menu_item_parent != $parent_id ): ?> </li> <?php $submenu = false; endif; $count++; endforeach; ?> </ul>
- The topic ‘WordPress menu, displaying parent child’ is closed to new replies.