There is such an example menu enter image description here

With this code, I want to display in addition to the main menu also a submenu in another place. When we are on page 1 , 1.1 , 1.3 or heading 1.2 , a submenu should be displayed, the same as heading 2 .

The source code was taken from this answer. I changed the structure a bit and corrected the line to display submenus on embedded pages / headings.

 $current_item = ($item->menu_item_parent) ? $item->menu_item_parent : $item->ID; 

Now the problem is that this code displays submenus for pages, but does not work with rubrics. What is the problem and how to fix it?

 add_filter( 'wp_nav_menu_objects', 'wp_nav_menu_objects_filter', 10, 2 ); function wp_nav_menu_objects_filter( $sorted_menu_items, $args ) { if ( 'Main' !== $args->menu ) { return $sorted_menu_items; } $items = array(); $current_item = null; foreach ( $sorted_menu_items as $item) { if ( in_array( 'current_page_item', $item->classes, true ) ) { $current_item = ($item->menu_item_parent) ? $item->menu_item_parent : $item->ID; continue; } } foreach ( $sorted_menu_items as $item) { if ( $current_item && intval( $item->menu_item_parent ) === intval( $current_item )) { $items[] = $item; continue; } } return $items; } 
  • Why these perversions? The VP menu is collected in the admin as Hosh. - SeVlad
  • @SeVlad, through the admin panel I have the main menu. I need to display submenus. - E_K
  • In the VP, you can create at least hundreds of menus and display them anywhere for any conditions you like. - SeVlad
  • @SeVlad, create 10+ menus and set up a bunch of conditions for the necessary headings, pages and don’t forget about 3 different languages, as for me it’s not the best option. - E_K
  • I don’t even know why it’s not optimal. The menu, by definition, “CMS” should be collected in the admin panel. But you can manage not only the menus, but also its individual items. There are even plugins for this. - SeVlad

0