0

I'm not really good with coding, but I have googled a lot and edit some script and it worked, but I can't achieve what I need.

This is the code:

class description_walker extends Walker_Nav_Menu
{
      function start_el(&$output, $item, $depth = 0, $args = Array(), $id = 0)
      {
           global $wp_query;
           $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

           $class_names = $value = '';

           $classes = empty( $item->classes ) ? array() : (array) $item->classes;

           $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
           $class_names = ' class="'. esc_attr( $class_names ) . '"';

           $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';

           $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
           $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
           $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
           $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';

           $prepend = '<strong>';
           $append = '</strong>';
           $description  = ! empty( $item->description ) ? '<span>'.esc_attr( $item->description ).'</span>' : '';

           if($depth != 0)
           {
                     $description = $append = $prepend = "";
           }

            $item_output = $args->before;
            $item_output .= '<a'. $attributes .'>';
            $item_output .= $args->link_before .$prepend.apply_filters( 'the_title', $item->title, $item->ID ).$append;
            $item_output .= $description.$args->link_after;
            $item_output .= '</a>';
            $item_output .= $args->after;

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

And In my .php file:

    <?php 
    wp_nav_menu( array(
     'container_class' => 'top-nav',
     'menu_class' => 'nav',
     'echo' => true,
     'before' => '',
     'after' => '',
     'link_before' => '',
     'link_after' => '',
     'depth' => 0,
     'theme_location' => 'topnav',
     'walker' => new description_walker())
     );
    ?>

The problem is I don't know how to add data for the container(the div) and the ul tag.

For example, this is what I'm getting:

<div class="top-nav">
<ul class="nav" ...>
<li ...></li>
</ul>
</div>

And I want something like this:

<div class="top-nav" mine="something">
<ul class="nav" ... mine2="something2">
<li ...></li>
</ul>
</div>

Please help me

1

Well, you wouldn't need a walker for that, if it's simply adding a data attribute or something like that you could just use the items_wrap parameter. View wp_nav_menu() in Resources:

$items_wrap = '<div class="top-nav" mine="something">';
$items_wrap .= '<ul id="%1$s" mine="something2" class="nav %2$s">%3$s</ul>';
$items_wrap .= '</div>';

wp_nav_menu( array(
    'container'         => false,
    'echo'              => true,
    'before'            => '',
    'after'             => '',
    'link_before'       => '',
    'link_after'        => '',
    'depth'             => 0,
    'theme_location'    => 'topnav',
    'items_wrap'        => $items_wrap,     // The Default is: '<ul id="%1$s" class="%2$s">%3$s</ul>'
    'walker'            => new description_walker()
) );

This is a more straight-froward way to do it than messing with a walker or something of the sort.

| improve this answer | |
  • 1
    Oh... My... GOSH! You're amazing!!! I was looking for this walker class like three hours :) Thank you so much!!! – cheanata Feb 3 '16 at 16:41

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.