How to add a custom item to a specific WordPress menu item position

后端 未结 4 1380
北荒
北荒 2020-12-30 16:48

I have a primary menu registered and displayed that consists of 4 links (home, about, news, blog). I want to add html (a logo) in between the second and third menu and I was

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-30 17:33

    I solved similar problem this way:

    add_filter('wp_nav_menu_items','add_custom_in_menu', 10, 2);
    
    function add_custom_in_menu( $items, $args ) 
    {
        if( $args->theme_location == 'primary' ) // only for primary menu
        {
            $items_array = array();
            while ( false !== ( $item_pos = strpos ( $items, 'custom HTML here
  • '); // insert custom item after 2nd one $items = implode('', $items_array); } return $items; }

    Please notice, it works only for single-level menu.

提交回复
热议问题