Michael Harmon
Michael Harmon

Reputation: 43

WordPress Menus - Don't Show Sub-Pages

I've created a custom side menu using the Wordpress > Appearance > Menu options. Here is the structure:

About Us
Our Leadership Team
    Name #1
    Name #2
    Name #3
Our Staff and Advisory Board
    Name #1
    Name #2
    Name #3
    Name #4

The menu shows on the pages that I want it to -- but it is showing completely expanded. The way I would like it to work : If I'm on "About Us" -- then only the top level sub-options are shown. (About Us, Our Leadership Team, Our Staff and Advisory Board)

If I click on "Our Leadership Team" then the three names under it are shown. Hope this makes sense.

This is the code I am using to call this menu from multiple pages:

                            <?php if( is_page(array(11,354,304,302,297,232,319,317,311,309)) ) :?>
                                <? wp_nav_menu( array('menu' => 'main-about' )); ?>
                            <?php endif;?>

Any help would be appreciated.

Upvotes: 0

Views: 2495

Answers (1)

Dave
Dave

Reputation: 892

I just finished a website where it was requred to have a main menu and a sub-menu in the sidebar

How I completed the task was to have the main menu where it was with a depth of 1 (only display the parent items). Note, change the 'menu' to your menu ID.

<?php wp_nav_menu( array( 'container_class' => 'menu', 'theme_location' => 'primary', 'depth' => '1', 'menu' => '3' ) ); ?>

Then all I had to do was post the child items. Instead of creating a menu, I had to dynamically generate the menu. So instead of having if or switch statements for each individual sub menus, this is the following piece of code I used. And it can be outside of the loop.

NOTE: This code was taken from the internet and modified, I don't know the original author.

<?PHP
// Get the parent's title (For display purpose)
$str_parenttitle = get_the_title($post->post_parent);

// This will display the child items of the parent
// And if it's a child item, display it's siblings
if($post->post_parent)
    $children = wp_list_pages("title_li=&sort_column=menu_order&child_of=".$post->post_parent."&echo=0&depth=1&exclude=73"); 
else
    $children = wp_list_pages("title_li=&sort_column=menu_order&child_of=".$post->ID."&echo=0&depth=1&exclude=73");
?>

Then display the menu

<?PHP if ($children && is_page()): ?>
    <ul class="menu">
        <li <?php if ( is_page() && $post->post_parent ) {} else { ?>class="current_page_item"<?php } // Shows the parent item on the sub menu ?>>
            <a href="<?php echo get_permalink($post->post_parent) ?>"><?php echo $str_parenttitle;?></a>
        </li>
        <?php echo $children; ?>
    </ul>
<?PHP endif; ?>

I hope this at least helps.

Upvotes: 1

Related Questions