WordPress菜单 - 不显示子页面

时间:2011-10-29 02:29:52

标签: wordpress

我使用Wordpress>创建了自定义侧边菜单外观>菜单选项。结构如下:

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

菜单显示在我想要的页面上 - 但它显示完全展开。我希望它的工作方式:如果我在“关于我们” - 那么只显示顶级子选项。 (关于我们,我们的领导团队,我们的员工和顾问委员会)

如果我点击“我们的领导团队”,那么会显示其下的三个名字。希望这是有道理的。

这是我用来从多个页面调用此菜单的代码:

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

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

我刚刚完成了一个网站,要求在侧边栏中设置主菜单和子菜单

我是如何完成任务的,主菜单的深度为1(仅显示父项)。注意,将“菜单”更改为菜单ID。

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

然后我所要做的就是发布子项目。我不得不动态生成菜单,而不是创建菜单。因此,不是每个子菜单都有if或switch语句,而是我使用的以下代码段。它可以在循环之外。

注意:此代码来自互联网并经过修改,我不知道原作者。

<?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");
?>

然后显示菜单

<?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; ?>

我希望这至少有帮助。

相关问题