{"id":1328,"date":"2014-07-01T15:41:00","date_gmt":"2014-07-01T06:41:00","guid":{"rendered":"http:\/\/www.santoshkori.com\/blog\/?p=1328"},"modified":"2014-07-01T15:42:32","modified_gmt":"2014-07-01T06:42:32","slug":"how-to-display-only-second-level-child-menu-in-wordpress","status":"publish","type":"post","link":"https:\/\/www.santoshkori.com\/blog\/how-to-display-only-second-level-child-menu-in-wordpress\/","title":{"rendered":"How to display only second level child menu in wordpress"},"content":{"rendered":"<p>Below is snippet code to display second level menu in wordpress.<br \/>\nPut below php code into your theme\/functions.php file<\/p>\n<pre>add_filter( 'wp_nav_menu_objects', 'my_wp_nav_menu_objects_sub_menu', 10, 2 );\r\n\r\n\/\/ filter_hook function to react on sub_menu flag\r\nfunction my_wp_nav_menu_objects_sub_menu( $sorted_menu_items, $args ) {\r\nif ( isset( $args-&gt;sub_menu ) ) {\r\n$root_id = 0;\r\n\/\/ find the current menu item\r\nforeach ( $sorted_menu_items as $menu_item ) {\r\nif ( $menu_item-&gt;current ) {\r\n\/\/ set the root id based on whether the current menu item has a parent or not\r\n$root_id = ( $menu_item-&gt;menu_item_parent ) ? $menu_item-&gt;menu_item_parent : $menu_item-&gt;ID;\r\nbreak;\r\n}\r\n}\r\n\/\/ find the top level parent\r\nif ( ! isset( $args-&gt;direct_parent ) ) {\r\n$prev_root_id = $root_id;\r\nwhile ( $prev_root_id != 0 ) {\r\nforeach ( $sorted_menu_items as $menu_item ) {\r\nif ( $menu_item-&gt;ID == $prev_root_id ) {\r\n$prev_root_id = $menu_item-&gt;menu_item_parent;\r\n\/\/ don't set the root_id to 0 if we've reached the top of the menu\r\nif ( $prev_root_id != 0 ) $root_id = $menu_item-&gt;menu_item_parent;\r\nbreak;\r\n}\r\n}\r\n}\r\n}\r\n\r\n$menu_item_parents = array();\r\nforeach ( $sorted_menu_items as $key =&gt; $item ) {\r\n\/\/ init menu_item_parents\r\nif ( $item-&gt;ID == $root_id ) $menu_item_parents[] = $item-&gt;ID;\r\n\r\nif ( in_array( $item-&gt;menu_item_parent, $menu_item_parents ) ) {\r\n\/\/ part of sub-tree: keep!\r\n$menu_item_parents[] = $item-&gt;ID;\r\n} else if ( ! ( isset( $args-&gt;show_parent ) &amp;&amp; in_array( $item-&gt;ID, $menu_item_parents ) ) ) {\r\n\/\/ not part of sub-tree: away with it!\r\nunset( $sorted_menu_items[$key] );\r\n}\r\n}\r\nreturn $sorted_menu_items;\r\n} else {\r\nreturn $sorted_menu_items;\r\n}\r\n}<\/pre>\n<p>Then you can display it in your theme using wp_nav_menu (just like you normally would), but also passing in a sub_menu flag to activate the custom sub_menu function:<\/p>\n<pre><!--?php   wp_nav_menu( array( 'theme_location' =--> 'primary', \/\/\/ Your primary theme location name as per theme code\r\n'sub_menu' =&gt; true\r\n) ); ?&gt;<\/pre>\n<h4>Incoming search terms:<\/h4><ul><li><a href=\"https:\/\/www.santoshkori.com\/blog\/how-to-display-only-second-level-child-menu-in-wordpress\/\" title=\"Howtodisplayonlysecondlevelchildmenuinwordpress|WordpressJoomlaFreelancerWebDesignerWebDeveloperMumbai\">Howtodisplayonlysecondlevelchildmenuinwordpress|WordpressJoomlaFreelancerWebDesignerWebDeveloperMumbai<\/a> (12)<\/li><\/ul>","protected":false},"excerpt":{"rendered":"<p>Below is snippet code to display second level menu in wordpress. Put below php code into your theme\/functions.php file add_filter( &#8216;wp_nav_menu_objects&#8217;, &#8216;my_wp_nav_menu_objects_sub_menu&#8217;, 10, 2 ); \/\/ filter_hook function to react on sub_menu flag function my_wp_nav_menu_objects_sub_menu( $sorted_menu_items, $args ) { if ( isset( $args-&gt;sub_menu ) ) { $root_id = 0; \/\/ find the current menu item [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[699,696,697,694,695,698],"class_list":["post-1328","post","type-post","status-publish","format-standard","hentry","category-wordpress","tag-display-only-second-level-child-menu-in-wordpress","tag-how-to-display-only-second-level-child-menu-in-wordpress","tag-how-to-get-only-second-level-child-menu-in-wordpress","tag-how-to-show-only-second-level-child-menu-in-wordpress","tag-how-to-show-only-second-level-child-sun-menu-in-wordpress","tag-show-only-second-level-child-menu-in-wordpress"],"_links":{"self":[{"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/posts\/1328","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/comments?post=1328"}],"version-history":[{"count":3,"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/posts\/1328\/revisions"}],"predecessor-version":[{"id":1331,"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/posts\/1328\/revisions\/1331"}],"wp:attachment":[{"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/media?parent=1328"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/categories?post=1328"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/tags?post=1328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}