wp_nav_menu のカスタマイズ

リンクタイトルに、説明文を付けたいという質問があったので、以下のように答えてみました。

functions.php

<?php
class MyWalker extends Walker_Nav_Menu{

	function start_el( &amp;$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;
		$classes[]= 'menu-item-' . $item->ID;

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

		$id= apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
		$id= $id ? ' id="' . esc_attr( $id ) . '"' : '';

		$output .= $indent . '<li' . $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 ) .'"' : '';

		$item_output= $args->before;
		$item_output .= '<a'. $attributes .'>';
		$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
		$item_output .= '</a>';
		$item_output .= '<span>'.$item->description.'</span>';
		$item_output .= $args->after;
		$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
	}

}?>

template.php

$MyWalker= new MyWalker();
$args= array('walker'=> $MyWalker);

wp_nav_menu( array('walker'=> $MyWalker, 'container_class'=> 'menu-header', 'theme_location'=> 'primary'));
?> 

http://ja.forums.wordpress.org/topic/10476?replies=5(b:title)

第2案は以下

<?php

add_filter( 'walker_nav_menu_start_el', 'my_test' ,10,4);

function my_test($item_output,$item,$depth,$args){
	return $item_output .'<span>'.$item->description.'</span>';
}

?>

nav_menu_css_class も ついている 便利だ。

残念だけれども、これらの事に気がついたのは、その質問があったからだった

[emulsion_relate_posts]