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.
create a file subMenu.php in theme folder add below code.
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 hookwp_nav_menu_items
is inside the listul
. 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.