0

I am trying to output another wp_nav_menu if an existing wp_nav_menu item has a specific class like this..

/* Custom Walker */
class IBenic_Walker extends Walker_Nav_Menu {

// Displays start of an element. E.g '<li> Item Name'
// @see Walker::start_el()
function start_el(&$output, $item, $depth=0, $args=array(), $id = 0) {
    $object = $item->object;
    $type = $item->type;
    $title = $item->title;
    $description = $item->description;
    $permalink = $item->url;

    $output .= "<li class='" .  implode(" ", $item->classes) . "'>";
    $output .= '<a href="' . $permalink . '">';
    $output .= $title;
    $output .= '</a>';

    /* If Item Class Is shop_nav */
    if (in_array("shop_nav", $item->classes)) {
        $output .= '<ul class="sub-menu">
                        ' . wp_nav_menu( array(
                                'theme_location' => 'col_1',
                                'menu_id'        => 'col_1',
                                'menu_class'     => 'col_1',
                                'container'      => '',     
                                ) ) . '
                    </ul>';
    }

    $output .= '</li>';

}
}

This is not working as expected, can anyone see where I am going wrong?

0

By default wp_nav_menu() outputs the navigation markup. If you want to store it or want to concatenate it with other string then you'd have to set the echo parameter to false (by default it's true).

$output .= '<ul class="sub-menu">
        ' . wp_nav_menu( array(
                'theme_location' => 'col_1',
                'menu_id'        => 'col_1',
                'menu_class'     => 'col_1',
                'container'      => '',
                'echo'           => false,
                ) ) . '
        </ul>';

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.