wordpress进阶教程(二十四):wordpress菜单中如何输出菜单的描述


在后台菜单设置页面,每个菜单项默认有6个或7个属性,如下图,其中有一项图像描述,一般的主题基本用不到这个,而且这个属性也是被隐藏了,至今阿树也没有用过,但是既然wordpress提供了这个属性,那么他的用处以及用法如何?作为一系列教程,还是有必要写一下。wordpress菜单项中图像描述属性

注意:本篇教程中代码参考自:http://www.wpbeginner.com/wp-themes/how-to-add-menu-descriptions-in-your-wordpress-themes/

一、显示图像描述项

在后台菜单设置页面,点击页面右上角的--“显示选项”,勾选里面的--“图像描述”,然后每个菜单项中都会出现这个图像描述字段的输入框。后台菜单显示图像描述
然后你可以为每个菜单项输入描述信息了,不过默认情况下,主题是不会显示菜单的描述信息的,要想显示这个描述信息,还需要一些步骤。添加描述信息

二、通过walker参数来改变输出

和上一篇教程一样,在主题中添加下面的类:

  1. class Menu_With_Description extends Walker_Nav_Menu {   
  2.     function start_el(&$output, $item, $depth, $args) {   
  3.         global $wp_query;   
  4.         $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';   
  5.         $class_names = $value = '';   
  6.         $classes = emptyempty( $item->classes ) ? array() : (array) $item->classes;   
  7.         $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );   
  8.         $class_names = ' class="' . esc_attr( $class_names ) . '"';   
  9.         $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';   
  10.         $attributes = ! emptyempty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';   
  11.         $attributes .= ! emptyempty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';   
  12.         $attributes .= ! emptyempty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';   
  13.         $attributes .= ! emptyempty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';   
  14.         $item_output = $args->before;   
  15.         $item_output .= '<a'. $attributes .'>';   
  16.         $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;   
  17.         $item_output .= '<br /><span class="sub">' . $item->description . '</span>';   
  18.         $item_output .= '</a>';   
  19.         $item_output .= $args->after;   
  20.         $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );   
  21.     }   
  22. }  
  23.  
  24.  
  25. <?php   
  26. $args = array(   
  27.     'walker' => new Menu_With_Description(), //注意是 new Menu_With_Description();   
  28.     'theme_location' => 'ashu-menu',   
  29. );   
  30. wp_nav_menu($args);   
  31. ?>

 

三、前台调用设置walker参数。

  1. class Menu_With_Description extends Walker_Nav_Menu {   
  2.     function start_el(&$output, $item, $depth, $args) {   
  3.         global $wp_query;   
  4.         $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';   
  5.         $class_names = $value = '';   
  6.         $classes = emptyempty( $item->classes ) ? array() : (array) $item->classes;   
  7.         $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );   
  8.         $class_names = ' class="' . esc_attr( $class_names ) . '"';   
  9.         $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';   
  10.         $attributes = ! emptyempty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';   
  11.         $attributes .= ! emptyempty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';   
  12.         $attributes .= ! emptyempty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';   
  13.         $attributes .= ! emptyempty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';   
  14.         $item_output = $args->before;   
  15.         $item_output .= '<a'. $attributes .'>';   
  16.         $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;   
  17.         $item_output .= '<br /><span class="sub">' . $item->description . '</span>';   
  18.         $item_output .= '</a>';   
  19.         $item_output .= $args->after;   
  20.         $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );   
  21.     }   
  22. }  
  23.  
  24.  
  25. <?php   
  26. $args = array(   
  27.     'walker' => new Menu_With_Description(), //注意是 new Menu_With_Description();   
  28.     'theme_location' => 'ashu-menu',   
  29. );   
  30. wp_nav_menu($args);   
  31. ?>

 

四、实际效果,阿树用twentyten主题测试菜单显示描述信息

五、美化。