Add page parent class to subpage parent page in nav menu

To add class current to current sub page parent in top nav menu or any other nav menu add the below hook to functions.php file:

function add_class_to_wp_nav_menu($classes){
global $wp_query;
$post = $wp_query->post;
$pageMenuID = $post->post_parent;
$menu_items = wp_get_nav_menu_items(‘top menu’);
foreach($menu_items as $page) {
if( $page->post_parent == $pageMenuID){
$currentMenuItem = $page->ID;
$parentArr = get_page_by_path(‘lavora-con-noi’);
if( $page->post_parent == $parentArr->ID){
$currentMenuItem = $page->ID;

// add the current page class to a specific menu item (replace ###).
if (in_array(‘menu-item-‘.$currentMenuItem, $classes)){
$classes[] = ‘current-menu-item current_page_parent’;
return $classes;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s