0

I need to pull a sub-menu depending on the parent page (or section) of the website. I have three sections (Our Inn, Our Restaurant, Activities) each requiring an individual submenu that my client will be able to edit via the native menus section of WordPress.

This is the code I have in my header.php to pull the menus.

<?php 
if (is_front_page()) {
    wp_nav_menu( array(
        'theme_location'  => 'our-inn-menu',
        'menu_id'         => 'sub-menu',
        'container_class' => 'sub-menu',
    ) );
} elseif ($post->post_parent == 4) {
    //Parent is Our Inn
    wp_nav_menu( array(
        'theme_location'  => 'our-inn-menu',
        'menu_id'         => 'sub-menu',
        'container_class' => 'sub-menu',
    ) );
} elseif ($post->post_parent == 6) {
    // Restaurant Page 
    wp_nav_menu( array(
        'theme_location'  => 'our-restaurant-menu',
        'menu_id'         => 'sub-menu',
        'container_class' => 'sub-menu',
    ) );
} elseif ($post->post_parent == 8) {
    // Activities Page 
    wp_nav_menu( array(
        'theme_location'  => 'activities-menu',
        'menu_id'         => 'sub-menu',
        'container_class' => 'sub-menu',
    ) );
} else {
    //Everything Else
    wp_nav_menu( array(
        'theme_location'  => 'our-inn-menu',
        'menu_id'         => 'sub-menu',
        'container_class' => 'sub-menu',
    ) );
}

The problem is that when I assign each menu in the backend, it only shows the Our Inn menu from the else clause and nothing else. When I remove the else clause, it shows a blank div.

  • Anyone out there that can lead me in the right direction? – Beau Beau Jan 3 '18 at 17:01
0

Have you assigned the menus to theme locations in wp-admin? If you look under Appearance > Menus, there is a Menu Settings section for each menu, and you should have the correct theme location checked under "display location."

You might also try simplifying your code. Perhaps for some reason WordPress has not yet identified which page/post/content it is on yet before your code runs. When things go sideways I like to break them down, something like:

<?php
if (is_front_page()) {
    echo 'Front Page';
} elseif ($post->post_parent == 4) {
    echo 'Child of Our Inn';
} elseif ($post->post_parent == 6) {
    echo 'Child of Restaurant';
} elseif ($post->post_parent == 8) {
    echo 'Child of Activities';
} else {
    echo 'WP did not identify what content it was on';
}
?>

Using simple echos instead of going into your real functions, you can determine for sure whether or not your conditions are being met. If the conditions are never met, then for some reason WP does not yet know what content it is pulling up, which may just mean there are multiple loops on the page or something of that nature.

At that point I would move the if/else statement into one of the individual theme files - say page.php - inside that template's main Loop just to verify, and once that works you can put your wp_nav_menu calls back into place and make sure they are appearing.

You might want to use a query debugging plugin to figure out why WP doesn't know what content it is showing at the point where you're trying to add your code. It might be that moving your code below wp_header() if it is not already below it could help.

  • I figured out that the statements work but for whatever reason they are just defaulting to whatever elseif statement is at the end. Does anyone have any insight on this? – Beau Beau Jan 5 '18 at 2:10
  • In the end I decided to ditch the idea of using the post_parent ID as a way to identify which menu to pull. This would not have worked easily for pages that were 3 levels deep. Instead I used is_category conditional tag and pulled the category ID for every page. Essentially the three sections of my website are represented by three categories. Just a cleaner solution even though there is one more extra step when creating a page. If anyone stumbles across this post and knows a solution to my original problem, I would still be interested in knowing how to fix it. Thanks! – Beau Beau Jan 5 '18 at 4:01

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.