WordPress - 第三级菜单/导航

时间:2012-12-24 18:09:43

标签: php wordpress navigation

我在WordPress中遇到了第3级导航菜单的问题。我正在使用以下代码输出3个导航级别:

    <?php $parents = wp_list_pages("title_li=&depth=1&echo=0&sort_column=menu_order");
        if($post->post_parent) {
            $siblings = wp_list_pages("title_li=&depth=1&child_of=".$post->post_parent."&echo=0&sort_column=menu_order");
            $children = wp_list_pages("title_li=&depth=1&child_of=".$post->ID."&echo=0&sort_column=menu_order");
        } else {
            $children = wp_list_pages("title_li=&depth=1&child_of=".$post->ID."&echo=0&sort_column=menu_order");
            $siblings = array();
        }
        if($parents) { ?>
            <ul class="topseiten">
            <?php echo $parents; ?>
            </ul>
        <?php }
        if($siblings) { ?>
            <ul class="unterseiten">
            <?php echo $siblings; ?>
            </ul>
        <?php }
        if($children) { ?>
            <ul class="unterunterseiten">
            <?php echo $children; ?>
            </ul>
        <?php } ?>

当我点击菜单1 - &gt;菜单2正在显示 - &gt;行

当我点击菜单2 - &gt;菜单3显示,您看到菜单1 - &gt;行

当我点击菜单3 - &gt;菜单2被隐藏了!

有人可以帮助我吗?非常感谢!

谢谢,圣诞快乐! 凯文

2 个答案:

答案 0 :(得分:0)

尝试使用以下代码替换第3行

$current_parent = $post->post_parent;
$parent_post = get_post( $current_parent );
$siblings = wp_list_pages("title_li=&depth=1&child_of=".$parent_post->post_parent."&echo=0&sort_column=menu_order");

答案 1 :(得分:0)

好的,找到了另一种现在完美的解决方案:

<?php
//Automatic Submenu
global $wp_query;
//Wenn die Seite ein Grandparent ist, also keine Eltern hat...
if( empty($wp_query->post->post_parent) ) 
  {
        //Herausfinden ob es Kinder gibt...
        $ich=$wp_query->post->ID;
        $children = wp_list_pages("title_li=&child_of=$ich&echo=0"); 
            if ($children) 
            {
                $parent1 = $wp_query->post->ID;
                //Menue ausgeben
                echo "<ul class='topseiten'>";
                wp_list_pages("title_li=&child_of=$parent1&depth=1");
                echo "</ul>";
            }
            else {
                echo "<ul class='topseiten'>";
                wp_list_pages("title_li=&depth=1");
                echo "</ul>";
            }
 } 

else
//hat Eltern, ist also ein parent 
 {
    $ich=$wp_query->post->ID;
    $children = wp_list_pages("title_li=&child_of=$ich&echo=0"); 
    //UND hat weitere Kinder
    if ($children) 
    {
        $parent1 = $wp_query->post->post_parent;
        //MENUE 1 mit Geschwistern aktueller Seite (Kinder von $parent1)
        echo "<ul class='topseiten'>";
        wp_list_pages("title_li=&depth=1");
        echo "</ul>";
        echo "<ul class='unterseiten'>";
        wp_list_pages("title_li=&child_of=$parent1&depth=1");
        echo "</ul>";

        $parent2 = $wp_query->post->ID;
        //Menue 2 mit Kindern aktueller Seite ($parent2)
        echo "<ul class='unterunterseiten'>";
        wp_list_pages("title_li=&child_of=$parent2&depth=1");
        echo "</ul>";

    }
    else
    {
            if(get_grandpapa(''))
            {
                $parent1 = get_grandpapa('');
                //MENUE 1 mit Geschwistern aktueller Seite (Kinder von $parent1)
                echo "<ul class='topseiten'>";
                wp_list_pages("title_li=&depth=1");
                echo "</ul>";
                echo "<ul class='unterseiten'>";
                wp_list_pages("title_li=&child_of=$parent1&depth=1");
                echo "</ul>";

                $parent2 = $wp_query->post->post_parent;
                //Menue 2 mit Kindern aktueller Seite ($parent2)
                echo "<ul class='unterunterseiten'>";
                wp_list_pages("title_li=&child_of=$parent2&depth=1");
                echo "</ul>";
            }
            else
            {
                $parent1 = $wp_query->post->post_parent;
                //NUR Menue 1 mit Geschwistern aktueller Seite (Kinder von $parent1)
                echo "<ul class='topseiten'>";
                wp_list_pages("title_li=&depth=1");
                echo "</ul>";
                echo "<ul class='unterseiten'>";
                wp_list_pages("title_li=&child_of=$parent1&depth=1");
                echo "</ul>";
            }
    }
}

?>

这个函数我必须放在主题的functions.php中:

function get_grandpapa($page_id){
$current_page = get_page( $page_id );
if ($current_page->post_parent > 0){
    //has at least a parent
    $parent_page = get_page($current_page->post_parent);
    if ($parent_page->post_parent > 0){
        return $parent_page->post_parent;
    }else{
        return false;
    }
}
return false; }