Instantly share code, notes, and snippets.
Forked from thefuxia/class.T5_Nav_Menu_Walker_Simple.php
Created
June 10, 2012 08:00
Star
You must be signed in to star a gist
Mi_Menu_Walker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php /** | |
* Create a nav menu with very basic markup. | |
* Based on Thomas Scholz http://toscho.de, T5_Nav_Menu_Walker_Simple | |
* | |
* @author César Hernández [cesarhdz.com] | |
* @version 1.0 | |
*/ | |
class Mi_menu_walker 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 ) | |
{ | |
/* | |
* Adds post_name as class | |
* 1. The title as class, to support portability, uses title, because post_title is not consitent | |
* 2. If has children (not currently supported) | |
* 3. Current regardless the level | |
*/ | |
$classes = array(); | |
$classes[] = sanitize_html_class(sanitize_title($item->title)); | |
$classes[] = ($item->current OR $item->current_item_ancestor OR $item->current_item_parent) | |
? 'current' : ''; | |
$output .= '<li class="' . trim(implode(' ', $classes)). '">'; | |
$attributes = ''; | |
/* | |
* Adds description if available, wrapped in small tags, because its an alternate way of calling the item | |
*/ | |
$description = ($item->description != '') ? "<p class=\"description\"><small>{$item->description}</small></p>" | |
: ''; | |
! 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>{$description}" | |
. "$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>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment