WordPress.org

Ready to get started?Download WordPress

Forums

A secondary menu for specific pages/subpages (3 posts)

  1. vinx77
    Member
    Posted 1 year ago #

    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 http://www.platformparallel.nl/.

  2. vinx77
    Member
    Posted 1 year ago #

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

  3. Andy Mercer
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic