$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>functions - Combine multiple menus using the filter wp_nav_menu_items|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

functions - Combine multiple menus using the filter wp_nav_menu_items

matteradmin14PV0评论

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);

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);
Share Improve this question asked Mar 21, 2019 at 17:06 JasonJason 2052 silver badges12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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>';
    }
Post a comment

comment list (0)

  1. No comments so far