0

Is there a way to combine multiple menus in the functions.php file using wp_nav_menu_items()? I need to hook in a second menu to the main menu. The site has two separate menus in the header. One is a drop down and the other is a hamburger. When on mobile the drop down is no good. I need to add the items from the drop down menu (menu 2) to the main menu (hamburger).

below is my current setup. As you can see I am already adding in social and company info to the menu. Now I need to add a second menu.

function add_social_to_wp_menu ( $items, $args ) {

    if ( 'on' === et_get_option( 'divi_show_facebook_icon', 'on' ) ) :
        $social_icons .= '<span class="et-social-icon et-social-facebook">
            <a href="'.esc_url( et_get_option( 'divi_facebook_url', '#' ) ).'" class="icon">
            </a>
        </span>';
    endif;
    if ( 'on' === et_get_option( 'divi_show_twitter_icon', 'on' ) ) :
        $social_icons .= '<span class="et-social-icon et-social-twitter">
            <a href="'.esc_url( et_get_option( 'divi_twitter_url', '#' ) ).'" class="icon">
            </a>
        </span>';
    endif;
    if ( 'on' === et_get_option( 'divi_show_google_icon', 'on' ) ) :
        $social_icons .= '<span class="et-social-icon et-social-google-plus">
            <a href="'.esc_url( et_get_option( 'divi_google_url', '#' ) ).'" class="icon">
            </a>
        </span>';
    endif;
    if ( 'on' === et_get_option( 'divi_show_instagram_icon', 'on' ) ) :
        $social_icons .= '<span class="et-social-icon et-social-instagram">
            <a href="'.esc_url( et_get_option( 'divi_instagram_url', '#' ) ).'" class="icon">
            </a>
        </span>';
    endif;
    if ( 'on' === et_get_option( 'divi_show_youtube_icon', 'on' ) ) :
        $social_icons .= '<span class="et-social-icon et-social-youtube">
            <a href="'.esc_url( et_get_option( 'divi_youtube_url', '#' ) ).'" class="icon">
            </a>
        </span>';
    endif;
    if ( 'on' === et_get_option( 'divi_show_rss_icon', 'on' ) ) :

        $et_rss_url = '' !== et_get_option( 'divi_rss_url' )
            ? et_get_option( 'divi_rss_url' )
            : get_bloginfo( 'rss2_url' );

        $social_icons .= '<span class="menu-item et-social-icon et-social-rss">
            <a href="'.esc_url( $et_rss_url ).'" class="icon">
            </a>
        </span>';
    endif;

    if( 'main-menu' === $args -> menu ) {
        $items .= '<li class="menu-item company-info">'.do_shortcode('[show-company-info]').'</li>';
        $items .= '<li class="menu-item social-networks">'.$social_icons.'</li>';
    }

    return $items;
}
add_filter('wp_nav_menu_items','add_social_to_wp_menu',10,2);
0

Thankful to the Lord, I found a solution for anyone else that comes across this issue. I took this function from the Wordpress codex and modified it to soot my needs

function get_menu($menu_name) {

    $menu_items = wp_get_nav_menu_items($menu_name);
    $menu_list = '';
    $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; 

            if(!empty($menu_items[$count + 1]) && $menu_items[ $count + 1 ]->menu_item_parent == $parent_id ) { //Checking for child

                $menu_list .= '<li class="menu-item menu-item-has-children"><a href="'.$link.'">'.$title.'</a>';

            }else{

                $menu_list .= '<li class="menu-item"><a href="'.$link.'">'.$title.'</a>';

            }

        }

        if ( $parent_id == $menu_item->menu_item_parent ) {

            if ( !$submenu ) {

                $submenu = true;
                $menu_list .= '<ul class="sub-menu">';

            }

            $menu_list .= '<li class="menu-item"><a href="'.$link.'">'.$title.'</a></li>';

            if(empty($menu_items[$count + 1]) || $menu_items[ $count + 1 ]->menu_item_parent != $parent_id && $submenu) {

                $menu_list .= '</ul>';
                $submenu = false;

            }

        }

        if (empty($menu_items[$count + 1]) || $menu_items[ $count + 1 ]->menu_item_parent != $parent_id ) { 

            $menu_list .= '</li>';      
            $submenu = false;

        }

        $count++;

    }

    return $menu_list;

}

I then called the function from my menu filter (see function above) like this.

if( 'main-menu' === $args -> menu ) {
        $items .= $fcs_menu = get_menu('SECOND MENU NAME GOES HERE');
        $items .= '<li class="menu-item company-info">'.do_shortcode('[show-company-info]').'</li>';
        $items .= '<li class="menu-item social-networks">'.$social_icons.'</li>';
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.