• I am trying to load a specific custom menu for each of a group of four core pages. The only change is the menu, so I don’t want to load a different sidebar for each but just make the correct menu appear. Works fine for the top page but won’t work for subpages.

    Here is the how the menu is being called:

    if (is_page('rabbit') || is_subpage(5)) {
    		wp_nav_menu( array('menu' => 'rabbit' ));
    	} elseif (is_page('hare') || is_subpage(9)) {
    		wp_nav_menu( array('menu' => 'hare' ));
    	// etc.
    	} else {
    		wp_nav_menu ( array('menu' => 'default' ));
    	}

    This is the is_subpage function:

    function is_subpage( $iID = null )
    	{
    		global $post, $wpdb;
    
    		if ( is_page() AND isset( $post->post_parent ) != 0 )
    		{
    			$aParent = $wpdb->get_row( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE ID = %d AND post_type = 'page' LIMIT 1", $post->post_parent ) );
    
    			if ( is_int( $iID ) > 0 )
    				if ( $aParent->ID == $iID ) return true; else return false;
    			else
    				if ( $aParent->ID )	return true; else return false;
    
    		}
    		else
    		{
    			return false;
    		}
    	}

    (courtesy of https://www.mattvarone.com/wordpress/wordpress-check-page-parent).

    This works fine except that some of my subpages use a different page template and then it just goes to the default.

    I can get around it by creating page templates and loading the appropriate sidebar, but seems silly when only a single element is changing. I would much prefer to do it programatically.

  • The topic ‘Trouble using Custom Menus on Sub Pages’ is closed to new replies.