WordPress menu with submenu code

WordPress dynamic menu source code:

File: functions.php

 

'footer-menu' => 'Footer Menu'
) );
}
add_action('init', 'register_my_menus');
?>

 

File: header.php

 

‘primary-menu’,
‘container’ => false,
‘items_wrap’ => ‘%3$s‘,
‘container_class’ => false,
‘container_id’ => false,
‘menu_class’ => ‘nav-menu’
));

 

Output:

WordPress menu with submenu code

WordPress dynamic submenu source code:

File: functions.php

$root_id = 0;
foreach ( $sorted_menu_items as $menu_item ) {
if ( $menu_item->current ) {
$root_id = ( $menu_item->menu_item_parent ) ? $menu_item->menu_item_parent : $menu_item->ID;
break;
}
}
if ( ! isset( $args->direct_parent ) ) {
$prev_root_id = $root_id;
while ( $prev_root_id != 0 ) {
foreach ( $sorted_menu_items as $menu_item ) {
if ( $menu_item->ID == $prev_root_id ) {
$prev_root_id = $menu_item->menu_item_parent;
if ( $prev_root_id != 0 ) $root_id = $menu_item->menu_item_parent;
break;
}
}
}
}
$menu_item_parents = array();
foreach ( $sorted_menu_items as $key => $item ) {
if ( $item->ID == $root_id ) $menu_item_parents[] = $item->ID;
if ( in_array( $item->menu_item_parent, $menu_item_parents ) ) {
$menu_item_parents[] = $item->ID;
} else if ( ! ( isset( $args->show_parent ) && in_array( $item->ID, $menu_item_parents ) ) ) {
unset( $sorted_menu_items[$key] );
}
}
return $sorted_menu_items;
} else {
return $sorted_menu_items;
}
}
add_filter( 'my_sub_menu', 'register_my_sub_menu', 10, 2 );
?>

File: direct_parent.php

'sub_menu' => true,
'direct_parent' => true
) );
?>

Leave a Reply

Your email address will not be published. Required fields are marked *