How to show only second level menu in wordpress ?

Open functions.php and add below code

function wp_nav_submenu($menu_name, $post_id, $direct_output = true)
{
if (($locations = get_nav_menu_locations()) && isset($locations[$menu_name])) {
$menu = wp_get_nav_menu_object($locations[$menu_name]);
$menu_items = wp_get_nav_menu_items($menu->term_id);

// first we get our current main menu entry id
foreach ($menu_items as $item) {
if ($item->object_id == $post_id) {
if ($item->menu_item_parent) {
$current_menu_id = $item->menu_item_parent;
} else {
$current_menu_id = $item->ID;
}
break;
}
}

if (!empty($current_menu_id)) {

// now we get every item that is our main menu entries decended
$subs = array();
foreach ($menu_items as $item) {
if ($item->menu_item_parent == $current_menu_id) {
$subs[] = $item;
}
}

ob_start();
// finaly we list our submenu entries
print '<ul class="sub-menu clearfix">';
foreach ($subs as $sub) {
$title = empty($sub->post_title) ? get_the_title($sub->object_id) : $sub->post_title;
$r = ($sub->object_id == $post_id) ? 'active' : '';
print "<li class='".$r."'>";
print "<a href='".get_permalink($sub->object_id)."'>";
print $title;
print "</a>";
print '</li>';

}
print '</ul>';
$output = ob_get_clean();
if ($direct_output) {
echo $output;
} else {
return $output;
}
}
}
}

then add below code in your page.php
wp_nav_submenu('menu_location', $post->ID);


Related Post


Latest Post


Recent Posts Widget

Make sure to never miss a thing...

Get the latest news from the creative industry along with other creative goodies, conveniently delivered to social media.