Skip to content

Instantly share code, notes, and snippets.

@thefuxia
Created June 29, 2011 08:49
Show Gist options
  • Star 35 You must be signed in to star a gist
  • Fork 21 You must be signed in to fork a gist
  • Save thefuxia/1053467 to your computer and use it in GitHub Desktop.
Save thefuxia/1053467 to your computer and use it in GitHub Desktop.
T5_Nav_Menu_Walker_Simple
<?php # -*- coding: utf-8 -*-
/**
* Create a nav menu with very basic markup.
*
* @author Thomas Scholz http://toscho.de
* @version 1.0
*/
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->attr_title )
// Avoid redundant titles
and $item->attr_title !== $item->title
and $attributes .= ' title="' . esc_attr( $item->attr_title ) .'"';
! empty ( $item->url )
and $attributes .= ' href="' . esc_attr( $item->url ) .'"';
$attributes = trim( $attributes );
$title = apply_filters( 'the_title', $item->title, $item->ID );
$item_output = "$args->before<a $attributes>$args->link_before$title</a>"
. "$args->link_after$args->after";
// 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( &$output )
{
$output .= '<ul class="sub-menu">';
}
/**
* @see Walker::end_lvl()
*
* @param string $output Passed by reference. Used to append additional content.
* @return void
*/
public function end_lvl( &$output )
{
$output .= '</ul>';
}
/**
* @see Walker::end_el()
*
* @param string $output Passed by reference. Used to append additional content.
* @return void
*/
function end_el( &$output )
{
$output .= '</li>';
}
}
@lonchbox
Copy link

Thanx for this gist, it help me a lot, but how can I also use the current-menu-item class in the

  • ?

  • @smhmic
    Copy link

    smhmic commented Jul 25, 2013

    @lonchbox - To allow a class for the current menu item, change the first line in start_el() to something like:

    $output .= '<li'.($item->current ? ' class="current"':'').'>';
    

    @Jakobs-House
    Copy link

    great stuff!! thanx for that...
    is there a possibility of adding the level nr. ?

    @Jakobs-House
    Copy link

    ok found it :)
    just added to the li output from smhmic

     'level-' . $depth . 

    (just dont know how to post a code here in github comment field :))

    @thehung2224
    Copy link

    Hi,

    Thank you for the code. But I can't insert new classes in css classes (optional)

    @vtrmage
    Copy link

    vtrmage commented Dec 31, 2018

    where do I put this 0n ?

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment