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.
<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.
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";
}
}