How to add sub-menu to a menu generated by wp_nav_menu by using plugin

I have a menu generated by wp_nav_menu which look like

<ul class="nav-menu" id="menu-top-nav">
    <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-43" id="menu-item-43"><a href="http://www.example.com/item1.com">Item 1</a></li>
    <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-44" id="menu-item-44"><a href="http://www.example.com/item2.com">Item 2</a></li>
    <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-45" id="menu-item-45"><a href="http://www.example.com/item3.com">Item 3</a></li>
    <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-46" id="menu-item-46"><a href="http://www.example.com/item4.com">Item 4</a></li>
</ul>

I want to modify above menu by adding a sub-menu to “Item 3” using my plugin, so below is my desired output.

Read More
<ul class="nav-menu" id="menu-top-nav"><li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-43" id="menu-item-43"><a href="http://www.example.com/item1.com">Item 1</a></li>
  <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-44" id="menu-item-44"><a href="http://www.example.com/item2.com">Item 2</a></li>
  <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-45" id="menu-item-45"><a href="http://www.example.com/item3.com">Item 3</a>
    <ul class="sub-menu">
      <li class="menu-item" id="menu-item-48"><a href="http://www.example.com/child1.com">child 1</a></li>
      <li class="menu-item" id="menu-item-49"><a href="http://www.example.com/child2.com">child 2</a></li>
      <li class="menu-item" id="menu-item-50"><a href="http://www.example.com/child3.com">child 3</a></li>
   </ul>
  </li>
  <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-46" id="menu-item-46"><a href="http://www.example.com/item4.com">Item 4</a></li>
</ul>

I have tried following filters but these didn’t helped me to achieve the above output.

wp_setup_nav_menu_item
wp_get_nav_menu_items
wp_nav_menu_items

Workaround 1:

add_filter('wp_nav_menu_items', 'my_custom_menu_item', 10, 2);

function my_custom_menu_item($items, $args)
{
    $parent_item_number = 3;
    $pos = nth_strpos($items, '</a>', $parent_item_number) + 4;
    $cat_id = 9;
    $args = array('numberposts' => 5, 'category' => $cat_id);
    $myposts = get_posts($args);
    if (!empty($myposts))
    {
        $str_to_insert = '<ul class="sub-menu">';
        global $post;
        foreach ($myposts as $post) : 
            setup_postdata($post);
            $str_to_insert .= '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
        endforeach;
        $str_to_insert .= '</ul>';

        $items = substr($items, 0, $pos) . $str_to_insert . substr($items, $pos);
    }
    return $items;
}

function nth_strpos($str, $substr, $n, $stri = false)
{
    if ($stri)
    {
        $str = strtolower($str);
        $substr = strtolower($substr);
    }
    $ct = 0;
    $pos = 0;
    while (($pos = strpos($str, $substr, $pos)) !== false)
    {
        if (++$ct == $n)
        {
            return $pos;
        }
        $pos++;
    }
    return false;
}

Above solution works but I think it’s not a proper way of achieving desired outcome. I’d love to have a good solution from you.

Related posts

Leave a Reply

2 comments

  1. You can modify your menu by using walker.

    include('subMenu.php');
    $menu =  wp_nav_menu( array('menu' => 'YOUR-MENU-NAME','menu_class' => 'megamenu','walker' => new subMenu));
    

    create a file subMenu.php in theme folder add below code.

    <?php
    class subMenu extends Walker_Nav_Menu {
        function end_el(&$output, $item, $depth=0, $args=array()) {
    
        if( 'Item 3' == $item->title ){
            $output .= '<ul class="sub-menu">
            <li class="menu-item" id="menu-item-48"><a href="http://www.example.com/child1.com">child 1</a></li>
            <li class="menu-item" id="menu-item-49"><a href="http://www.example.com/child2.com">child 2</a></li>
            <li class="menu-item" id="menu-item-50"><a href="http://www.example.com/child3.com">child 3</a></li>
            </ul>';
        }
        $output .= "</li>n";  
        }
    }
    
  2. The Hook wp_nav_menu_items is right if you will add items to the structure of WP Nav Menu.
    See the the exmample below for add a static “Home” link. The Link is static, from the function home_url() and I empbed this only in the arguments from WP Nav Menu fpr the values before and after. The li-item is static, is only chnaged in WordPress, if you use a Walker for this element. The hook wp_nav_menu_items is inside the list ul. After define the var for this new item content I add this to the default list in the var $items, set before the list output. Now only a return of all content.

    add_filter( 'wp_nav_menu_items', 'fb_add_home_link', 10, 2 );
    function fb_add_home_link( $items, $args ) {
    
        $home_item =
                '<li>' .
                $args->before .
                '<a href="' . home_url( '/' ) . '" title="Home">' .
                $args->link_before . __( 'Home' ) . $args->link_after .
                '</a>' .
                $args->after .
                '</li>';
    
        $items = $home_item . $items;
    
        return $items;
    }