Create Custom Menu In WordPress

function clean_custom_menus() {
$menu_name = ‘top’; // specify custom menu slug
if (($locations = get_nav_menu_locations()) && isset($locations[$menu_name])) {
$menu = wp_get_nav_menu_object($locations[$menu_name]);
$menu_items = wp_get_nav_menu_items($menu->term_id);//echo ‘<pre>’;
//print_r($menu_items); //$menu_list = ‘<nav>’ .”\n”;
$menu_list .= ‘<ul class=”nav navbar-nav”>’; $count = 0;
$submenu = false; foreach( $menu_items as $menu_item ) { $link = $menu_item->url;
$title = $menu_item->title; if ( !$menu_item->menu_item_parent ) {
$parent_id = $menu_item->ID; $menu_list .= ‘<li class=”nav-item dropdown”>’ .”\n”;
$menu_list .= ‘<a href=”‘.$link.'” class=”dropdown-toggle” data-toggle=”dropdown” data-hover=”dropdown” data-delay=”0″ data-close-others=”false”>’.$title.'<i class=”fa fa-angle-down”></i></a>’ ;
} if ( $parent_id == $menu_item->menu_item_parent ) { if ( !$submenu ) {
$submenu = true;
$menu_list .= ‘<ul class=”dropdown-menu”>’ ;
} $menu_list .= ‘<li class=”item”>’ .”\n”;
$menu_list .= ‘<a href=”‘.$link.'” class=”title”>’.$title.'</a>’ .”\n”;
$menu_list .= ‘</li>’ ; if ( $menu_items[ $count + 1 ]->menu_item_parent != $parent_id && $submenu ){
$menu_list .= ‘</ul>’;
$submenu = false;
} } if ( $menu_items[ $count + 1 ]->menu_item_parent != $parent_id ) {
$menu_list .= ‘</li>’;
$submenu = false;
} $count++;
} $menu_list .= ‘</ul>’; } else {
// $menu_list = ‘<!– no list defined –>’;
}
echo $menu_list;
}

Leave a Reply

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