3
<div class="example">
<h1>Menu</h1>
<ul>
    <li>sub-menu</li>
    <li>sub-menu</li>
    <li>sub-menu</li>
</ul>
</div><!-- end of example -->

<div class="example>
<h1>Menu</h1>
<ul>
    <li>sub-menu</li>
    <li>sub-menu</li>
    <li>sub-menu</li>
</ul>
</div><!-- end of example -->

I'm trying to make a menu with wp_nav_menu() and I want to achieve a clean code like this. I'm trying to do it with a custom walker but I don't have experience with this. I think I have to overwrite a class but ...

Coud you help me with the code?? Thanks for all

dani
  • 131
  • 1
  • 4

1 Answers1

3

In my basis theme I use a walker for a simplified output:

<?php # -*- coding: utf-8 -*-
/**
 * Create a nav menu with very basic markup.
 *
 */
class T5_Nav_Menu_Walker_Simple extends Walker_Nav_Menu
{
    /**
     * Start the element output.
     *
     * @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. May be used for padding.
     * @param  array $args    Additional strings.
     * @return void
     */
    public function start_el( &$output, $item, $depth, $args )
    {
        $output     .= '<li>';
        $attributes  = '';
    ! empty ( $item-&gt;attr_title )
        // Avoid redundant titles
        and $item-&gt;attr_title !== $item-&gt;title
        and $attributes .= ' title=&quot;' . esc_attr( $item-&gt;attr_title ) .'&quot;';

    ! empty ( $item-&gt;url )
        and $attributes .= ' href=&quot;' . esc_attr( $item-&gt;url ) .'&quot;';

    $attributes  = trim( $attributes );
    $title       = apply_filters( 'the_title', $item-&gt;title, $item-&gt;ID );
    $item_output = &quot;$args-&gt;before&lt;a $attributes&gt;$args-&gt;link_before$title&lt;/a&gt;&quot;
                    . &quot;$args-&gt;link_after$args-&gt;after&quot;;

    // Since $output is called by reference we don't need to return anything.
    $output .= apply_filters(
        'walker_nav_menu_start_el'
        ,   $item_output
        ,   $item
        ,   $depth
        ,   $args
    );
}

/**
 * @see Walker::start_lvl()
 *
 * @param string $output Passed by reference. Used to append additional content.
 * @return void
 */
public function start_lvl( &amp;$output )
{
    $output .= '&lt;ul class=&quot;sub-menu&quot;&gt;';
}

/**
 * @see Walker::end_lvl()
 *
 * @param string $output Passed by reference. Used to append additional content.
 * @return void
 */
public function end_lvl( &amp;$output )
{
    $output .= '&lt;/ul&gt;';
}

/**
 * @see Walker::end_el()
 *
 * @param string $output Passed by reference. Used to append additional content.
 * @return void
 */
function end_el( &amp;$output )
{
    $output .= '&lt;/li&gt;';
}

}

See this answer for implementation details.

fuxia
  • 107,219
  • 39
  • 255
  • 462