I did this by following steps:
Step 1: First creating a default primary menu. Navigate to Appearance > Menus and create a default primary menu for your site. Check the “Primary Navigation Menu” checkbox and save this menu.
Step 2: Creating another menu for pages only. Navigate to Appearance > Menus and create another menu for your pages.
Step 3: Now add this simple code in functions.php file
function gd_nav_menu_args( $args ){ if( ( 'primary' == $args['theme_location'] ) && is_singular('page') ) { $args['menu'] = 'Menu for Page'; // Add your menu name here. My case it is "Menu for Page" } return $args; } add_filter( 'wp_nav_menu_args', 'gd_nav_menu_args' );
is_singular('page')
conditional tag will return true when user is visiting a page. You can change this conditional tag as per your requirement.