Reply To: Multiple primary navigations?

Home Forums Support Multiple primary navigations? Reply To: Multiple primary navigations?

Home Forums Support Multiple primary navigations? Reply To: Multiple primary navigations?

#200484
Tommi

Hello, just to report on my progress.

I’ve managed to bend GeneratePress into changing the menus without any additional plugins. Here’s how:

First and foremost. I have 3 navigation areas. Primary navigation has pages that are considered top level pages in hierarchy. Read more about it here: codex.wordpress.org. One of these pages we shall call “site-area-1” which is a parent or an ancestor to every page under that topic. This is crucial for the longer code below to work, without it the if statement won’t function as intended and the entire thing falls apart.

all the code in this version is inserted into functions.php file in your child theme.

Start by registering the new navigation menus. In my case I wanted the primary menu and two additional menus to be changed at will:


register_nav_menus( array(
	'site-area-1'       => __( 'menuName1', 'myChildTheme'),
	'site-area-2'       => __( 'menuName2', 'myChildTheme)
) );

Following this, and here is the rather rough part (It’ll probably need a lot of cleanup). We hijack the function “generate_navigation_position()” from themes/generatepress/inc/navigation.php and declare it like so:


function generate_navigation_position()
{
	?>
	<nav itemtype="http://schema.org/SiteNavigationElement" itemscope="itemscope" id="site-navigation" <?php generate_navigation_class(); ?>>
		<div class="inside-navigation grid-container grid-parent">
			<?php do_action( 'generate_inside_navigation' ); ?>
			<button class="menu-toggle" aria-controls="primary-menu" aria-expanded="false">
				<?php do_action( 'generate_inside_mobile_menu' ); ?>
				<span class="mobile-menu"><?php echo apply_filters('generate_mobile_menu_label', __( 'Menu', 'generatepress' ) ); ?></span>
			</button>
			<?php 

			/* Get current ID and get it's ancestors. */
				global $post;
				$ancs = get_ancestors($post->ID, 'page');
				/*Ancestors returns an array. */

				if( end($ancs) == 90 || is_page(90)) {
					/* check value of array index 0 against ancestor's page ID */
					wp_nav_menu( 
						array( 
							'theme_location' => 'site-area-1', 
							/* Add registered menu to 'theme_location' => 'YOUR_MENU' */
							'container' => 'div',
							'container_class' => 'main-nav',
							'container_id' => 'primary-menu',
							'menu_class' => '',
							'fallback_cb' => 'generate_menu_fallback',
							'items_wrap' => '<ul id="%1$s" class="%2$s ' . join( ' ', generate_get_menu_class() ) . '">%3$s</ul>'
						) 
					);
				} 

				elseif( end($ancs) == 92 || is_page(92)) {

					wp_nav_menu( 
						array( 
							'theme_location' => 'site-area-2',
							'container' => 'div',
							'container_class' => 'main-nav',
							'container_id' => 'primary-menu',
							'menu_class' => '',
							'fallback_cb' => 'generate_menu_fallback',
							'items_wrap' => '<ul id="%1$s" class="%2$s ' . join( ' ', generate_get_menu_class() ) . '">%3$s</ul>'
						) 
					);
				}
				else{
					wp_nav_menu( 
						array( 
							'theme_location' => 'primary',
							'container' => 'div',
							'container_class' => 'main-nav',
							'container_id' => 'primary-menu',
							'menu_class' => '',
							'fallback_cb' => 'generate_menu_fallback',
							'items_wrap' => '<ul id="%1$s" class="%2$s ' . join( ' ', generate_get_menu_class() ) . '">%3$s</ul>'
						) 
					);
			} 
			?>
		</div><!-- .inside-navigation -->
	</nav><!-- #site-navigation jalla jalla -->
	<?php
}

With this I got it to function. Every page that is a grand child of page ID 90 will get site-area-1 navigation and every page that is a grandchild of page ID 92 will get site-area-2 navigation. For pages that are neither they’ll get the primary menu from the Else segment.

Known problems:
Carets (down arrows for dropdowns) and search field do not function in site-area-1 and 2 navigations, yet.

  • This reply was modified 5 years ago by Tommi. Reason: typos within the code
  • This reply was modified 5 years ago by Tommi.