BootstrapPHPWordPress

How to fetch custom menu and links in WordPress

0
901
2017 / 11 / 23

WordPress customization is always been interested for the web developers. In this article we will learn how to create a custom menu and fetch it in our website. I have written the code in such a way that you can fetch both Parent and their children menu items.

In our example, let we have created our custom menu and name it as “Custom Menu”.

Once you are done with the menu and its items, save it.

 

Now the coding part

$menu_name = 'Custom Menu';
if ( $menu_name != '' ) {
    $menu = wp_get_nav_menu_object($menu_name);
  
    $menu_items = wp_get_nav_menu_items($menu->term_id);
    $menu_list = '';
    $count = 0;
    $submenu = false;$cpi=get_the_id();
    foreach( $menu_items as $current ) {
        if($cpi == $current->object_id ){if ( !$current->menu_item_parent ) {$cpi=$current->ID;}else{$cpi=$current->menu_item_parent;}$cai=$current->ID;break;}
    }
    foreach( $menu_items as $menu_item ) {
        $link = $menu_item->url;
        $title = $menu_item->title;
        $menu_item->ID==$cai ? $ac2=' current_menu' : $ac2='';
        if ( !$menu_item->menu_item_parent ) {
            $parent_id = $menu_item->ID;$parent_id==$cpi ? $ac=' current_item' : $ac='';
            if(!empty($menu_items[$count + 1]) && $menu_items[ $count + 1 ]->menu_item_parent == $parent_id ){//Checking has child
                $menu_list .= '<li class="dropdown has_child'.$ac.'"><a href="'.$link.'" class="dropdown-toggle'.$ac2.'" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><span class="nav-span"></span>'.$title.'<span class="caret"></span></a>';
            }else{
                $menu_list .= '<li class="'.$ac.'">' ."\n";$menu_list .= '<a href="'.$link.'" class="'.$ac2.'">'.$title.'</a>' ."\n";
            }
             
        }
        if ( $parent_id == $menu_item->menu_item_parent ) {
            if ( !$submenu ) {
                $submenu = true;
                $menu_list .= '<ul class="dropdown-menu">' ."\n";
            }
            $menu_list .= '<li class="item">' ."\n";
            $menu_list .= '<a href="'.$link.'" class="'.$ac2.'">'.$title.'</a>' ."\n";
            $menu_list .= '</li>' ."\n";
            if(empty($menu_items[$count + 1]) || $menu_items[ $count + 1 ]->menu_item_parent != $parent_id && $submenu){
                $menu_list .= '</ul>' ."\n";
                $submenu = false;
            }
        }
        if (empty($menu_items[$count + 1]) || $menu_items[ $count + 1 ]->menu_item_parent != $parent_id ) { 
            $menu_list .= '</li>' ."\n";      
            $submenu = false;
        }
        $count++;
    }
} else {
    $menu_list = '<li>Menu "' . $menu_name . '" not defined.</li>';
}

echo $menu_list;

Thats it.

I have written in Bootstrap dropdown menu structure. The code is fully customizable and you can change classes to make it workable for accordians or with your theme CSS classes.

 

Trick:

You can write this code where ever needed on your theme, you can put that code in a function e.g. fetch_custom_menu($menu_name) and put the function code in the functions.php file and just call the function with the menu name e.g. fetch_custom_menu('Custom Menu').

Hi, I am a full-stack web developer with 5+ years of experience in working with different web technologies. Do you want to ask something? just send me a message through the contact form. Please visit my portfolio at hamzamehmood.com. Thanks