添加div并使用wordpress walker更改类或导航菜单?

时间:2013-11-13 09:38:57

标签: php html css wordpress navigation

我想知道如何制作一个WordPress wp_nav_menu()或wp_list_pages()Walker类,它将生成这样的导航菜单输出?     

<!-- Level1 -->
    <li class="current">
            <a href="#">Name</a>
<!--Level2-->
            <div class="nav-sub">
                    <ul>
                            <li><a href="#">Name</a></li>
                            <li>
                                    <a href="#">Name</a>
<!--Level3-->
                                    <div class="nav-sub">
                                            <ul>
                                                    <li>
                                                            <a href="#">Name</a>
<!--Can Add Unlimited levels If Possible-->
                                                    </li>
                                                    <li><a href="#">Name</a>
                                            </li>
                                            </ul>
                                    </div>
                            </li>
                            <li><a href="#">Name</a></li>
                            <li><a href="#">Name</a></li>
                    </ul>
            </div>
    </li>

    <!-- This is One Level only -->
    <li>
            <a href="#">Name</a>
    </li>

我尝试了很多方法,但不能只想出来。 这是使用wp_list_pages()

的当前输出
<ul class="myclass">
<li id="home"><a href="/index.php">Home</a></li>
<li class="page_item page-item-2 page_item_has_children"><a href="http://localhost/wordpress/sample-page/">Sample Page</a>
<ul class="children">
<li class="page_item page-item-9"><a href="http://localhost/wordpress/sample-page/home/">Home</a></li>
</ul>
</li>
<li class="page_item page-item-19"><a href="http://localhost/wordpress/page-no-sub/">page no sub</a></li>
</ul>

提前致谢!

P.S:我已经在这里阅读了这篇文章http://codex.wordpress.org/Function_Reference/wp_nav_menu但是无法弄清楚:(

1 个答案:

答案 0 :(得分:1)

  

编辑:

     

好吧,查看不同的参数并调用菜单

<?php
class Child_Wrap extends Walker_Nav_Menu
{
    function start_lvl(&$output, $depth)
    {
        $indent = str_repeat("\t", $depth);
        $output .= "\n$indent<div class=\"div-sub\"><ul class=\"sub-menu\">\n";
    }
    function end_lvl(&$output, $depth)
    {
        $indent = str_repeat("\t", $depth);
        $output .= "$indent</ul></div>\n";
    }
}

wp_nav_menu( 


    array( 
        'menu' => 'navigation',//(i created this menu on the backend of wordpress)
        'container'       => 'div',
        'container_class' => 'menu-sidebar', 
        'menu_class'      => 'menu-class',
        'menu_id'         => 'menu-id',
        'theme_location' => 'primary', 
        'walker' => new Child_Wrap() 

    ) 



 ); ?>

您可以指定许多参数(id菜单,类和id容器等...)

source class walker

Should you find your happiness here

Name menu screen ('menu' => 'navigation')