• I wanted to share my solution for getting a menu for certain pages. I have a webpage with events, a blog and general pages. The blog is shared over all events, but the event-pages needed their own menu.

    So I wanted to have a WP3-menu shown only when in the tree of a certain event. My solution is “if the name of the parent equals to the name of a menu, then show that menu. If there is no such menu defined or when on the top-page, show nothing”. For each event I make a new WP3-menu with exactly the same name as the event’s first page – the below code allows me to show only a menu if that part of the page has a menu defined. So for instance, the about-page does not trigger it.

    <?php
    	// get the parent-page - this page defines the 'region'
    	$parents = get_post_ancestors( $post->ID );
    	$region_id = ($parents) ? $parents[count($parents)]: $post->ID;
    	// get the name from the id
    	$region = get_page( $region_id );
    	$region_name = $region->post_name;
    	//echo "[".$region_name."]";
    	// show the menu when it exists
    	if (strlen($region_name)>0 && wp_get_nav_menu_object($region_name) ) {
    	// change below to fit your own need. Only 'menu' is important
    	wp_nav_menu( array('menu' => $region_name, 'container' => '', 'container_class' => '', 'menu_class' => '', 'menu_id' => 'menuhead', 'sort_column' => 'menu_order') );
    }
    ?>

    As you see, finally a good reason to have multiple menus. Hopefully I have helped somebody. To know how it looks like, take a look at https://www.platformparallel.nl/.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter vinx77

    (@vinx77)

    Excuse me:
    ‘$region_id = ($parents) ? $parents[count($parents)-1]: $post->ID;`

    Another way to do this is just copy page.php into several template files. Change the name of the menu called on each. Then when you publish each page, select the template.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘A secondary menu for specific pages/subpages’ is closed to new replies.