【问题标题】:WordPress - 3rd Level Menu/NavigationWordPress - 3 级菜单/导航
【发布时间】:2012-12-11 01:24:42
【问题描述】:

我在 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 -> 显示菜单 2 -> 确定

当我点击菜单 2 -> 菜单 3 正在显示并且你看到菜单 1 -> 确定

当我点击菜单 3 -> 菜单 2 被隐藏!

有人可以帮我吗?真的很感激!

谢谢,圣诞快乐! 凯文

【问题讨论】:

    标签: php wordpress navigation


    【解决方案1】:

    尝试用以下代码替换第 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");
    

    【讨论】:

    • 感谢弗拉基米尔的回答。可悲的是,它不适用于您的线路。 :-(
    • 很遗憾我无法测试代码,因为我没有你的数据。你看到了哪些兄弟姐妹?
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2017-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-18
      • 2019-09-08
      • 2017-03-30
      • 2015-07-22
      相关资源
      最近更新 更多