Abdus Sattar Bhuiyan
Abdus Sattar Bhuiyan

Reputation: 3074

Walker_Nav_Menu is not working

my wp_nav_manu function is here:

   <?php 
      wp_nav_menu(
                 array(
                            'theme_location' =>'main_nav',

                            'walker' => new customize_primary_menu()
                        )
      ); ?>

the walker class is :

<?php 
class customize_primary_menu extends Walker_Nav_Menu
{
    //echo 'sattarNaz'; exit;
    function start_lvl($output, $depth = 0, $arg = array() ){
        echo 'here';exit;
        $indent = str_repeat("\t", $depth);
        //print_r($output); exit;
       $output = "\n$indent<ul class= \"sattar-menu\">\n";

    } 
}

Here I put this code just to see the sattar-menu as testing class. But when I inspect the DOM no such class found!. I recheck my code again and again. Any help will be great!

Upvotes: 2

Views: 1817

Answers (1)

Lemon Kazi
Lemon Kazi

Reputation: 3311

In your template where you want to display this menu place this code.

 if ( has_nav_menu( 'main_nav' ) ) {

         $defaults = array(
            'theme_location'  => 'main_nav',
            //'menu'            => '',
             'container'       => 'ul',
             'container_class' => '',
             'container_id'    => '',
             'menu_class'      => '',
             'menu_id'         => '',
             'walker'          =>  new customize_primary_menu()
        );

        wp_nav_menu( $defaults );

    }

Then in your function you can try this

public function start_lvl( &$output, $depth = 0, $args = array() ) {
    $indent = str_repeat("\t", $depth);
    $output .= "\n$indent<ul class=\"sub-menu dropdown-menu\">\n";
  }

Also you have to create function start_el to customize menu. Look at this answer, it explains how to add custom HTML to the wordpress menus: https://stackoverflow.com/a/12251157/1627227

After you have this in place, you have to go to the point where your menu (wp_nav_menu()) is called. In the answer I've linked to, there's the full function call to wp_nav_menu. However you'll have to add this line: 'walker' => new customize_primary_menu to the arguments array, to use your custom walker object on that specific menu.

Hope you got it ;)

Upvotes: 1

Related Questions