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