自定义子菜单导航Walker

时间:2019-07-09 17:08:36

标签: wordpress drop-down-menu menu wp-nav-walker

我正在使用自定义的导航浏览器,并想创建一个树形菜单。

这是完整导航的HTML结构

<ul class="navbar-nav">
                            <li class="has_dropdown">
                                <a href="index.html">HOME</a>
                                <div class="dropdowns dropdown--menu">
                                    <ul>
                                        <li>
                                            <a href="index.html">Home Multi Vendor</a>
                                        </li>
                                        <li>
                                            <a href="index-single.html">Home Two Single User</a>
                                        </li>
                                        <li>
                                            <a href="index3.html">Home Three Product</a>
                                        </li>
                                    </ul>
                                </div>
                            </li>
                            <li class="has_dropdown">
                                <a href="all-products-list.html">all product</a>
                                <div class="dropdowns dropdown--menu">
                                    <ul>
                                        <li>
                                            <a href="all-products.html">Recent Items</a>
                                        </li>
                                        <li>
                                            <a href="all-products.html">Popular Items</a>
                                        </li>
                                        <li>
                                            <a href="index3.html">Free Templates</a>
                                        </li>
                                        <li>
                                            <a href="#">Follow Feed</a>
                                        </li>
                                        <li>
                                            <a href="#">Top Authors</a>
                                        </li>
                                    </ul>
                                </div>

这是wp_nav_menu的HTML调用:

  <?php 
                    $args = array(

                        'container'       => 'ul',
                        'theme_location'  => 'primary-menu',
                        'menu_class'      => 'navbar-nav',


                       );
                       wp_nav_menu( $args );
    ?>              

我该如何使用Walker功能

1 个答案:

答案 0 :(得分:0)

有关WordPress NavWalker Class

的更多信息

要将步行者添加到菜单中,您将在代码中包括步行者类,并通过将其函数名作为参数传递给菜单,从而将其应用于菜单:

主题模板文件的导航菜单输出

$args = [
  'container'       => 'ul',
  'theme_location'  => 'primary-menu',
  'menu_class'      => 'navbar-nav',
  'walker'          => new Walker_Texas_Ranger()
];
wp_nav_menu( $args );

实际的nav-walker类(位于主题的functions.php或其他地方)

class Walker_Texas_Ranger extends Walker {

    // Tell Walker where to inherit it's parent and id values
    var $db_fields = array(
        'parent' => 'menu_item_parent', 
        'id'     => 'db_id' 
    );

    /**
     * At the start of each element, output a <li> and <a> tag structure.
     * 
     * Note: Menu objects include url and title properties, so we will use those.
     */
    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
        $output .= sprintf( "\n<li><a href='%s'%s>%s</a></li>\n",
            $item->url,
            ( $item->object_id === get_the_ID() ) ? ' class="current"' : '',
            $item->title
        );
    }

}