Asia

How to add submenu items programmatically in WordPress?

17 December 2021

Appending an item to the existing menu can be done using the wp_nav_menu_objects hook. The submenu can be created using the current menu structure.

In the submenu code snippet, we need to add the specific ID to become the child of the specific menu.

There are specific requirements we need to follow to include the items in the submenu. The requirements are defined in the array and the array which include the title for the submenu, Unique ID, Parent menu item, URL for redirection.

Code Snippet

The snippet is to be added in the functions.php

add_filter( 'wp_nav_menu_objects', 'ravs_add_menu_parent_class' );
 function ravs_add_menu_parent_class( $items ) {
    foreach ( $items as $item ) {
         print_r($item);//print each menu item an get your parent menu item-id
    }
   $link = array (
       'title'            => 'add your menu title here',
       'menu_item_parent' =>add here parent menu item,
       'ID'               => 'add here unique ID',
       'url'              => 'www.google.com'
  );
  $items[] = (object) $link;
  return $items;
}

Tudip Technologies

Tudip Technologies is a value-driven Software Services company. We deliver excellence in dedicated and business-centric web solutions incorporated with innovation and expertise as the tools to achieve business goals. Our high-quality, cost-effective, reliable, and scalable services have helped us build an enduring relationship with our clients and customers.

Latest Posts

  • All Posts
  • Casestudy
  • Cloud
  • Healthcare
  • Ios
  • IOT
  • Java
  • Manufacturing
  • News
  • OTT
  • Supply Chain
  • Support and Maintainance
    •   Back
    • Banking
Cloud-Powered Agility

December 27, 2024

27 December 2024 Introduction: Transforming Chaos into Opportunity Imagine a supply chain where disruptions are predicted before they occur, inventory…

Cloud-Powered Agility

December 24, 2024

Imagine a supply chain where disruptions are predicted before they occur, inventory is optimized in real-time, and every stakeholder works…

Related Posts

  • All Posts
  • Healthcare