0

所以我遇到了一些问题,我不知道如何解决它们,我最近开始工作,WordPress navwalkers我被困在一个特定的部分,我不知道如何解决它。

所以我在 wordpress 主题中使用了 Bulma 框架。

这是我当前的输出:

在此处输入图像描述

这是我试图实现的目标(如果你有任何批评,请给我批评,我还在学习) - 我只是不知道如何在第一个 div 之后添加一个额外的 div 来包装$depth

在此处输入图像描述

这是我的wp_nav_menu标题:

<div class="container">
            <div class="navbar-menu is-active">
                    <div class="navbar-start" id="main-navigation">
                        <?php
                        wp_nav_menu( [
                            'theme_location' => 'primary',
                            'depth' => 3,
                            'container' => true,
                            'items_wrap' => '%3$s', // Removes the <ul>
                            'walker' => new Nav_walker(),
                        ]);
                        ?>
                    </div>
            </div>
        </div>

这是导航行者代码

class Nav_walker extends Walker_Nav_Menu
{
    /**
     * Starts the list before the elements are added
     *
     * @see Walker::start_lvl()
     *
     * @param string $output
     * @param int $depth
     * @param array $args
     */
    public function start_lvl(&$output, $depth = 0, $args = [])
    {
        // Indent the items
        $indent = str_repeat("\t", $depth);

        // The first <div> inside mega-menu
        if ($depth == 0) {
            $output .= "\n$indent<div class='navbar-dropdown'>\n";
        } else {
            $output .= "<div id=\"nav-items\">";
        }
    }

    /**
     * Ends the list of after the elements are added
     *
     * @see Walker::end_lvl()
     *
     * @param string $output
     * @param int $depth
     * @param array $args
     */
    public function end_lvl(&$output, $depth = 0, $args = [])
    {
        $indent = str_repeat("\t", $depth);
        $output .= "$indent</div>\n";
    }

    /**
     * Start the element output
     *
     * @param string $output
     * @param WP_Post $item
     * @param int $depth
     * @param array $args
     * @param int $id
     */
    public function start_el(&$output, $item, $depth = 0, $args = [], $id = 0)
    {
        $indent = ($depth) ? str_repeat("\t", $depth) : '';

        // Define all the menu-item classes
        $classes = empty($item->classes) ? [] : (array) $item->classes;

        // Combine the class names
        $class_names = implode(' ', $item->classes);

        // Add in the navbar-item class
        $class_names .= ' navbar-item';

        // Add in the active class to current menu item
        if (in_array('current-menu-item', $classes)) {
            $class_names .= ' active';
        }

        // Check if there are children
        $hasChildren = $args->walker->has_children;

        // If it's the first depth, apply the needed classes
        if ($depth === 0) {
            $class_names .= $hasChildren ? " has-dropdown is-hoverable" : "";
        }

        // If it's the second depth, apply the needed classes
        if ($depth === 1) {
            $class_names .= ' column is-3';
        }

        // Apply the needed class markup
        $class_names = $class_names ? 'class="' . esc_attr($class_names) . '"' : '';

        // If there are children, continue to apply elements
        if ($hasChildren) {
            $output .= "<div " . $class_names . ">";

            if ($depth === 0) {
                $output .= "<div class='navbar-link'>" . $item->title . "</div>";
            } else {
                $output .= "<h1 class='title is-6 is-mega-menu-title'>" . $item->title . "</h1>";
            }
        }
        else {
            $output .= "<a class='navbar-item' href='" . $item->url . "'>" . $item->title;
        }

        // Adds has_children class to the item so end_el can determine if the current element has children
        if ($hasChildren) {
            $item->classes[] = 'has_children';
        }
    }

    /**
     * Ends the element output
     *
     * @param string $output
     * @param WP_Post $item
     * @param int $depth
     * @param array $args
     * @param int $id
     */
    public function end_el(&$output, $item, $depth = 0, $args = array(), $id = 0 )
    {
        if (in_array("has_children", $item->classes)) {
            $output .= "</div>";
        }
        $output .= "</a>";
    }
}
4

0 回答 0