問題描述
普通 Wordpress 選單看起來像:
Home | Blog | About us | Contact
但是我看到很多頁面都有這些連結的描述:
Home Page | Our Blogs | About us
| Contact
….meet us…| read more| basic info| contact form
如何實現這一點?
(我希望它成為我所有主題的核心功能,所以沒有外掛,我只想知道它是如何完成的)
最佳解決方案
您需要導航選單的自定義步行者。
基本上,您將'walker'引數新增到 wp_nav_menu()選項,並呼叫增強類的例項:
wp_nav_menu(
array (
'menu' => 'main-menu',
'container' => FALSE,
'container_id' => FALSE,
'menu_class' => '',
'menu_id' => FALSE,
'depth' => 1,
'walker' => new Description_Walker
)
);
Description_Walker 類擴充套件了 Walker_Nav_Menu,並更改了 start_el( &$output, $item, $depth, $args )的功能,以查詢 $item->description 。
一個基本的例子:
/**
* Create HTML list of nav menu items.
* Replacement for the native Walker, using the description.
*
* @see https://wordpress.stackexchange.com/q/14037/
* @author toscho, http://toscho.de
*/
class Description_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|object $args Additional strings. Actually always an
instance of stdClass. But this is WordPress.
* @return void
*/
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 )
{
$classes = empty ( $item->classes ) ? array () : (array) $item->classes;
$class_names = join(
' '
, apply_filters(
'nav_menu_css_class'
, array_filter( $classes ), $item
)
);
! empty ( $class_names )
and $class_names = ' class="'. esc_attr( $class_names ) . '"';
$output .= "<li id='menu-item-$item->ID' $class_names>";
$attributes = '';
! empty( $item->attr_title )
and $attributes .= ' title="' . esc_attr( $item->attr_title ) .'"';
! empty( $item->target )
and $attributes .= ' target="' . esc_attr( $item->target ) .'"';
! empty( $item->xfn )
and $attributes .= ' rel="' . esc_attr( $item->xfn ) .'"';
! empty( $item->url )
and $attributes .= ' href="'%20%20%20.%20esc_attr(%20$item->url%20%20%20%20%20%20%20%20)%20.'"';
// insert description for top level elements only
// you may change this
$description = ( ! empty ( $item->description ) and 0 == $depth )
? '<small class="nav_desc">' . esc_attr( $item->description ) . '</small>' : '';
$title = apply_filters( 'the_title', $item->title, $item->ID );
$item_output = $args->before
. "<a $attributes>"
. $args->link_before
. $title
. '</a> '
. $args->link_after
. $description
. $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
);
}
}
或者 as @nevvermind commented,您可以繼承父級 start_el 功能的所有功能,只需將描述附加到 $output:
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 )
{
parent::start_el( $output, $item, $depth, $args );
$output .= sprintf(
'<i>%s</i>',
esc_html( $item->description )
);
}
樣品輸出:
現在啟用 wp-admin/nav-menus.php 中的描述欄位可以編輯該欄位。如果你不 WP 只是將完整的帖子內容刪除。
進一步閱讀:
- Associated bugs
- Similar example with different markup and formatting
- Enable description box in menu management screen programmatically
- Collect the item description for later usage
就是這樣
次佳解決方案
由於 WordPress 3.0,您不再需要自定義步行者了!
有 walker_nav_menu_start_el 過濾器,參見 https://developer.wordpress.org/reference/hooks/walker_nav_menu_start_el/
例:
function add_description_to_menu($item_output, $item, $depth, $args) {
if (strlen($item->description) > 0 ) {
// append description after link
$item_output .= sprintf('<span class="description">%s</span>', esc_html($item->description));
// insert description as last item *in* link ($input_output ends with "</a>{$args->after}")
//$item_output = substr($item_output, 0, -strlen("</a>{$args->after}")) . sprintf('<span class="description">%s</span >', esc_html($item->description)) . "</a>{$args->after}";
}
return $item_output;
}
add_filter('walker_nav_menu_start_el', 'add_description_to_menu', 10, 4);
第三種解決方案
這不比其他建議好或壞; 它只是不同它也很短,也很甜。
不要將描述欄位用作 @toscho 建議,您可以使用所需的文字填寫每個選單項上的”Title” 欄位,然後使用此 CSS:
.menu-item a:after { content: attr(title); }
它也很容易使用 jQuery 附加它,但文字是足夠裝飾,CSS 似乎是適當的。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。

