ยป Download the Plugin | Support Forum
Genesis comes with two built-in navigation menus: Primary and Secondary. On most sites people only use the Primary and list subpages as a dropdown. But this plugin allows the secondary menu to be auto-populated with the current section’s children pages.
So if you have a site structure like this:
- Home
- About
- About Our Team
- About Our Company
- Company History
- Contact
The Home and Contact pages will not show a secondary menu. If you are on About, About Our Company, About Our Team, or Company History it will show the secondary navigation (Company History will be a dropdown under About Our Company).
I’ve used this on a few projects so thought I’d turn it into a plugin. I plan to be sharing a lot more little bits of functionality as plugins rather than tutorials so that I can improve it even when I’m done with the client’s website.
For advanced users, there’s a filter that let’s you modify the arguments that are passed to wp_list_pages. If you don’t want any dropdowns coming off the secondary menu, you could do this:
/**
* Limit Subnav to one level
*
* @author Bill Erickson
* @link https://www.billerickson.net/genesis-subpages-as-secondary-menu
*
*/
add_filter('be_genesis_subpages_args', 'be_filter_subpages');
function be_filter_subpages($args){
$additions = array( 'depth' => '1');
return array_merge( $args, $additions );
}
One client wanted the secondary nav to only show secondary pages (using the above code), and a third nav to show up if there’s tertiary pages. Here’s how to add a third navigation:
/**
* Add Third Navigation
*
* @author Bill Erickson
* @link https://www.billerickson.net/genesis-subpages-as-secondary-menu
*
*/
add_action('genesis_after_header', 'be_third_nav');
function be_third_nav() {
if( is_page() ) {
// Set standard args
$args = array(
'title_li' => '',
'echo' => false,
'depth' => '1'
);
// Get current location
wp_reset_query();
global $post;
// Don't run on top level page
if( empty( $post->post_parent ) )
return;
$parent = get_post( $post->post_parent );
// If on a secondary page, get children of current page
if ( empty( $parent->post_parent ) )
$args['child_of'] = $post->ID;
// If on tertiary page, get children of parent
else
$args['child_of'] = $parent->ID;
// Build menu
$menu = wp_list_pages( $args );
if ( !empty( $menu ) ) echo '<div id="thirdnav"><div class="wrap">'.$menu.'</div></div>';
}
}
Dave says
Bill, this is brilliant. I’ve been struggling with trying to add a tertiary nav, and this works perfectly. Thanks!
Darren Burns says
Bill,
Thanks for all your tutorials. They have really helped me start learning the ins and outs of WordPress and Genesis.
Don’t know if this is the right place for this but I made a small mod to the plug-in to prevent the subnav appearing if there are no child pages and wanted to run it by you to make sure I have done it correctly.
if (!empty($subnav))
$subnav_output = sprintf( ‘%2$s%1$s%3$s’, $subnav, genesis_structural_wrap( ‘subnav’, ”, 0 ), genesis_structural_wrap( ‘subnav’, ‘‘, 0 ) );
else
$subnav_output = ”;
Thanks again.
Bill Erickson says
Great recommendation. I’m implementing it into the plugin now
Bill Erickson says
Check the readme in a few minutes and you’ll the see the update. Since it’s not a major issue I didn’t bump up the version number
Sebastian says
Does this still work?
I tried to use the plugin first and then tried the code above to get anything running.
Even limiting the depth to 1 does nothing.
Bill Erickson says
I just tested on a clean installation with WP 5.0.3 and Genesis 2.8.1 and it still works great. Make sure your theme has a secondary menu location.
You should see this in the backend (screenshot) and this on the frontend (screenshot).