0

I am sure this has been asked. It's new territory for me so I don't have the full vocabulary of what I'm doing, nor am I sure this is the right way to do it.

I've got a bunch of internal pages on a site that the only real change from page to page is the tertiary nav I've got in a sidebar div. (And obviously the main content area).

I've got a bunch of theme_locations setup for wp_nav_menu. What's the best way to display them based on page ID or parent ID or something like that?

Something along these lines I found, but it's not quite what I need:

<?php
    $thispageid = get_the_ID();
    if( $thispageid == "1" ) )
        wp_nav_menu( array( 'theme_location' => 'tertiary_surgeons_consulting' ) );
    if( $thispageid == "2" ) )
        wp_nav_menu( array( 'theme_location' => 'tertiary_surgeons_consulting' ) ); 
    else
        wp_nav_menu( array( 'theme_location' => 'tertiary_surgeons_education' ) );
?>

Is this thought process the right way to accomplish this, or am I missing some other way that's already in WordPress?

5
  • Are you just looking for a nav of the respective subpages on each page? – deflime Apr 10 '15 at 21:03
  • 1
    Sorry, I'm mobile at the moment. Have you looked at is_page()? It takes arrays too for multiple pages: if( is_page( array(1,2) ) ), or if( $post->ID == 1 ) – deflime Apr 10 '15 at 21:11
  • 2
    Basically, this is along the right lines, if I understand you, though there may be ways to write neater or more maintainable code. What exactly isn't "quite what you need"? – s_ha_dum Apr 10 '15 at 22:18
  • @s_ha_dum So I guess the part that isn't where I want it to be with hard-coding the template with specific IDs is if the cliet were to add new pages, we'd have to go back into the code to add the new IDs. That's why I was kind of hoping there was a way to use parent page. – Keefer Apr 11 '15 at 0:33
  • @deflime, basically, yes. I need to explore is_page, but hoping in a perfect world for a more dynamic solution, possibly using the parent page as the indicator of the display theme_location – Keefer Apr 11 '15 at 1:08
1

The best solution would be to allow a user to choose the nav menu location when adding/editing a page. Use a meta box with a dropdown (select) with all the available locations, and then save the selected location in a custom field:

/**
 * Register the meta box.
 */
function wpse_183868_page_nav_menu_meta_box_register() {
    add_meta_box(
        'wpse_183868_page_nav_menu',
        'Nav Menu',
        'wpse_183869_page_nav_menu_meta_box',
        'page',
        'side'
    );
}

add_action( 'add_meta_boxes', 'wpse_183868_page_nav_menu_meta_box_register' );

/**
 * Display the meta box.
 */
function wpse_183869_page_nav_menu_meta_box( $post ) {
    $selected = get_post_meta( $post->ID, '_page_nav_menu', true );
    wp_nonce_field( 'save', 'page_nav_menu_nonce' );
    ?>

<select name="page_nav_menu">
    <?php foreach ( get_registered_nav_menus() as $name => $label ) : ?>
         <option value="<?php echo $name ?>"<?php selected( $name, $selected ) ?>><?php echo $label ?></option>
    <?php endforeach ?>
</select>

<?php
}

/**
 * Save the meta box data.
 */
function wpse_183869_page_nav_menu_save( $post_id ) {
    if ( ! isset( $_POST['page_nav_menu'], $_POST['page_nav_menu_nonce'] ) || ! wp_verify_nonce( $_POST['page_nav_menu_nonce'], 'save' ) )
        return;

    $nav_menus = get_registered_nav_menus();
    $nav_menu = $_POST['page_nav_menu'];

    if ( isset( $nav_menus[ $nav_menu ] ) ) // Paranoid - check the submitted value is definitely valid
        update_post_meta( $post_id, '_page_nav_menu', $nav_menu );
}

add_action( 'save_post', 'wpse_183869_page_nav_menu_save' );

Now in your template you can just use:

if ( ! $page_nav_menu = get_post_meta( get_the_ID(), '_page_nav_menu', true ) )
    $page_nav_menu = 'default_location';

wp_nav_menu( array( 'theme_location' => $page_nav_menu ) );
1
  • Works great @TheDeadMedic. But now have another feature enhancement. In the template, output, I want to echo an h3 with the value of the secondary level page (the subnav you helped with is the tertiary level). Is there a simple way to do that? – Keefer Apr 13 '15 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.