bhadaway / blankslate

A naked WordPress theme, powering over 10k websites. Buy me a sandwich. 🥪
https://opencollective.com/blankslate
328 stars 92 forks source link

Error with Nav-walker #28

Closed imjustabillr closed 2 years ago

imjustabillr commented 3 years ago

I'm using this theme, and I'm trying to add my own custom nav-walker. The problem is, when trying to use that nav-walker, I run into errors. Below is that class I am using. At this point I'm trying to get it to display. The error I'm receiving is "Invalid arguments passed" on the first line that it tries to use $item->classes. If I do a dump of item, it has the page info (ID, post-author, post date, etc...) but it doesn't have any of the things necessary for a menu item, like url, classes or title. Also, my menu items that aren't posts/pages don't come through at all. (I made a custom link that goes to google for a test).

class My_Menu_Walker extends Walker_Nav_Menu {
    function start_el(&$output, $item, $depth=0, $args=[], $id=0) {
        //var_dump($item);

        $output .= "<li class='" .  implode(" ", $item->classes) . "'>";
        if ($item->url && $item->url != '#') {
            $output .= '<a href="' . $item->url . '">';
        } else {
            $output .= '<span>';
        }
        $output .= $item->title;
        if ($item->url && $item->url != '#') {
            $output .= '</a>';
        } else {
            $output .= '</span>';
        }

    }
}

I'm adding the call to navwalker in header.php. I replaced the default call with a call to mine

   wp_nav_menu( array(
        'theme_location' => 'primary',
        'menu_class' => 'main-menu',
        'walker' => new My_Menu_Walker(),
    ));

Is there any reason you can think of that this would ne happening with this theme? If I try a generic theme like twenty-twenty, the same code above works.

Thank you.

bhadaway commented 3 years ago

Try changing the menu_class to something other than main-menu.