0

I am using the WP_navwalker for a dropdown menu. This works fine, but it only shows me the first and second level items. So de dropdown on the first level and a regular second item when dropdown is open. The third level is not visible. I want to display it under my second level items.

I am not skilled enough to properly edit the walker to fit make it fit my needs. can anyone help me?

The naw_walker code:

<?php

/**
 * Menu Walker
 *
 * @see Walker::start_lvl()
 * @since 3.0.0
 *
 * @param string $output Passed by reference. Used to append additional content.
 * @param int    $depth Depth of page. Used for padding.
 * @param object $args Arguments.
 */
public function start_lvl(&$output, $depth = 0, $args = array())
{
    $indent = str_repeat("\t", $depth);
    $output .= "\n$indent<ul role=\"menu\" class=\" dvt-nav-dropdown closed\">\n";
}

/**
 * Bootstrap Menu Walker start
 *
 * @see Walker::start_el()
 * @since 3.0.0
 *
 * @param string $output Passed by reference. Used to append additional content.
 * @param object $item Menu item data object.
 * @param int    $depth Depth of menu item. Used for padding.
 * @param object $args Arguments.
 * @param int    $id Menu item ID.
 */
public function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0)
{
    // Set level of indent.
    $indent = ($depth) ? str_repeat("\t", $depth) : '';

    // Create nav menu item.
    $nav_menu_atts = array();
    $nav_menu_atts['id'] = apply_filters('nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args);
    $nav_menu_atts['class'] = empty($item->classes) ? array() : (array) $item->classes;
    $nav_menu_atts['class'] = apply_filters('nav_menu_css_class', $nav_menu_atts['class'], $item, $args);

    // Create link attributes.
    $nav_menu_link_atts = array();
    $nav_menu_link_atts['title']  = ! empty($item->title)  ? $item->title  : '';
    $nav_menu_link_atts['target'] = ! empty($item->target) ? $item->target : '';
    $nav_menu_link_atts['rel']    = ! empty($item->xfn)    ? $item->xfn    : '';

    // If item has_children add atts to a.
    $nav_menu_link_atts['href'] = $args->has_children && 0 === $depth ? '#' : (!empty($item->url) ? $item->url : '');

    $nav_menu_link_atts = apply_filters('nav_menu_link_attributes', $nav_menu_link_atts, $item, $args);

    // Output LI menu item.
    $output .= $indent;
    $output .= '<li';
    // Output menu attributes.
    foreach ($nav_menu_atts as $attr => $value) {
        // Output attribute name.
        $output .= ' ' . esc_attr($attr);
        if (!empty($value)) {
            $output .= '="';
            // Add switch to handle escaping.
            switch ($attr) {
                case 'class':
                    $output .= esc_attr(join(' ', array_filter($value)));
                    break;
                default:
                    $output .= esc_attr($value);
                    break;
            }
            $output .= '"';
        }
    }
    $output .= '>';

    // Output A link.
    $item_output = $args->before;
    $item_output .= '<a';
    // Output link attributes.
    foreach ($nav_menu_link_atts as $attr => $value) {
        if (!empty($value)) {
            // Output attribute name.
            $item_output .= ' ' . esc_attr($attr) . '="';
            // Add switch to handle escaping.
            switch ($attr) {
                case 'href':
                    $item_output .= esc_url($value);
                    break;

                case 'class':
                    $item_output .= esc_attr(join(' ', array_filter($value)));
                    break;

                default:
                    $item_output .= esc_attr($value);
                    break;
            }
            $item_output .= '"';
        }
    }
    $item_output .= '>';
    $item_output .= $args->link_before . apply_filters('the_title', $item->title, $item->ID) . $args->link_after;
    $item_output .= '</a>';
    $item_output .= $args->after;

    $output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
}

/**
 * Traverse elements to create list from elements.
 *
 * Display one element if the element doesn't have any children otherwise,
 * display the element and its children. Will only traverse up to the max
 * depth and no ignore elements under that depth.
 *
 * This method shouldn't be called directly, use the walk() method instead.
 *
 * @see Walker::start_el()
 * @since 2.5.0
 *
 * @param object $element Data object.
 * @param array  $children_elements List of elements to continue traversing.
 * @param int    $max_depth Max depth to traverse.
 * @param int    $depth Depth of current element.
 * @param array  $args Arguments.
 * @param string $output Passed by reference. Used to append additional content.
 * @return null Null on failure with no changes to parameters.
 */
public function display_element($element, &$children_elements, $max_depth, $depth, $args, &$output)
{
    if (! $element) {
        return;
    }

    $id_field = $this->db_fields['id'];

    // Display this element.
    if (is_object($args[0])) {
        $args[0]->has_children = ! empty($children_elements[ $element->$id_field ]);
    }

    parent::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
}

/**
 * Menu Fallback
 * =============
 * If this function is assigned to the wp_nav_menu's fallback_cb variable
 * and a manu has not been assigned to the theme location in the WordPress
 * menu manager the function with display nothing to a non-logged in user,
 * and will add a link to the WordPress menu manager if logged in as an admin.
 *
 * @param array $args passed from the wp_nav_menu function.
 */
public static function fallback($args)
{
    if (current_user_can('manage_options')) {
        $fb_output = null;

        if ($args['container']) {
            echo '<' . esc_html($args['container']);

            if ($args['container_id']) {
                echo ' id="' . esc_attr($args['container_id']) . '"';
            }

            if ($args['container_class']) {
                echo ' class="' . esc_attr($args['container_class']) . '"';
            }

            echo '>';
        }

        echo '<ul';

        if ($args['menu_id']) {
            echo ' id="' . esc_attr($args['menu_id']) . '"';
        }

        if ($args['menu_class']) {
            echo ' class="' . esc_attr($args['menu_class']) . '"';
        }

        echo '>';
        echo '</ul>';

        if ($args['container']) {
            echo '</' . esc_html($args['container']) . '>';
        }
    }
}

Sorry for my English, thanks in advance!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.