菜单项说明?wp_nav_menu()的自定义Walker
3 个回答
- 投票数
-
- 2015-02-23
由于 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);
Since WordPress 3.0, you don't need a custom walker anymore!
There is the
walker_nav_menu_start_el
filter, see https://developer.wordpress.org/reference/hooks/walker_nav_menu_start_el/Example:
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的nav walker解决方案,但这更干净,更易于维护,这应该是公认的答案,更好的做法.Nice! I was using the nav walker solution by @toscho, but this is much cleaner and easier to maintain.This should be the accepted answer, much better practice.
- 1
- 2015-07-08
- Ronaldt
-
- 2012-04-18
这并不比其他建议好或坏;只是不同而已.它又短又甜.
您可以代替在 @toscho 建议的字段中使用"标题"字段每个菜单项都带有所需的文本,然后使用此CSS:
.menu-item a:after { content: attr(title); }
使用jQuery 来附加它也很容易,但是文本的装饰性足以使CSS看起来合适
This isn't better or worse than other suggestions; it's just different. It's short and sweet too.
Rather than using the description field as @toscho suggests, you could fill in the "Title" field on each menu item with the text you want, and then use this CSS:
.menu-item a:after { content: attr(title); }
It would also be easy to use jQuery to append it, but the text is ornamental enough that CSS seems appropriate.
-
-
嗯,这是一个简单易用的解决方案,但是如果您将其设为" span",为什么还要使用它呢?xhtml/html4不允许链接中包含块元素,但是html5允许,因此只需使用`div`,而无需任何CSS!Well its a simple and easy solution but why use `span` if you make it block anyway? xhtml/html4 not allows block elements inside links, html5 however does, so just use `div`, and no need for any css!
- 2
- 2013-03-05
- James Mitch
-
正常的Wordpress菜单如下:
但是我在这些链接下看到了很多带有说明的页面:
如何实现?
(我希望它成为我所有主题的核心功能,所以请没有插件,我只想知道它是如何完成的)