自定义子菜单导航 Walker

Custom submenu 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>

这是 HTML 对 wp_nav_menu 的调用:

  <?php 
                    $args = array(

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


                       );
                       wp_nav_menu( $args );
    ?>              

如何使用 Walker 函数执行此操作

这里有关于 WordPress NavWalker Class

的更多信息

要将助行器添加到您的菜单,您可以在代码中包含 walker-class 并通过将其 function-name 作为参数传递来将其应用于菜单:

nav-menu 主题的 template-files

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

实际 nav-walker class(在您的主题 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
        );
    }

}