admin管理员组文章数量:1314326
I need to hide certain nav items from the menu (based on the password protection functionality) and I'm having trouble getting my custom Walker to work. In fact, I can't even get the standard Walker_Nav_Menu to work when I add it via a filter.
I've created a plugin as follows:
<?php
function myplugin_page_menu_args( $args ) {
$args['walker'] = new Walker_Nav_Menu;
return $args;
}
add_filter( 'wp_page_menu_args', 'myplugin_page_menu_args' );
?>
There's no other code in the plugin file, just those 6 lines. I've added this plugin to a brand new install of WordPress (/) and the menu rendering is completely broken - all the items are on the same level, and none of them have any text. The right number of <li>
elements are generated, and the ids are correct, but the rest is wrong.
I've tried using a custom class with a .walk() method, as so:
class myplugin_walker {
function walk ( $elements, $to_depth ) {
return 'test';
}
}
function myplugin_page_menu_args( $args ) {
$args['walker'] = new myplugin_walker;
return $args;
}
add_filter( 'wp_page_menu_args', 'myplugin_page_menu_args' );
This works exactly as expected - the menu renders as 'test'. But trying to use either my own class which extends Walker_Nav_Menu
, or the core Walker_Nav_Menu
class itself, results in a flat list of empty <li>
tags.
I've tried a few other ways to achieve the same result - I've tried adding filters for wp_nav_menu_objects
and wp_get_nav_menu_items
but neither of my functions seem to get called at all.
Am I barking up the wrong tree here - is this something that won't work as part of a plugin, and needs to be elsewhere in the code? Or am I just missing something obvious?
I need to hide certain nav items from the menu (based on the password protection functionality) and I'm having trouble getting my custom Walker to work. In fact, I can't even get the standard Walker_Nav_Menu to work when I add it via a filter.
I've created a plugin as follows:
<?php
function myplugin_page_menu_args( $args ) {
$args['walker'] = new Walker_Nav_Menu;
return $args;
}
add_filter( 'wp_page_menu_args', 'myplugin_page_menu_args' );
?>
There's no other code in the plugin file, just those 6 lines. I've added this plugin to a brand new install of WordPress (http://dev.matt-howe.co.uk/wordpress/) and the menu rendering is completely broken - all the items are on the same level, and none of them have any text. The right number of <li>
elements are generated, and the ids are correct, but the rest is wrong.
I've tried using a custom class with a .walk() method, as so:
class myplugin_walker {
function walk ( $elements, $to_depth ) {
return 'test';
}
}
function myplugin_page_menu_args( $args ) {
$args['walker'] = new myplugin_walker;
return $args;
}
add_filter( 'wp_page_menu_args', 'myplugin_page_menu_args' );
This works exactly as expected - the menu renders as 'test'. But trying to use either my own class which extends Walker_Nav_Menu
, or the core Walker_Nav_Menu
class itself, results in a flat list of empty <li>
tags.
I've tried a few other ways to achieve the same result - I've tried adding filters for wp_nav_menu_objects
and wp_get_nav_menu_items
but neither of my functions seem to get called at all.
Am I barking up the wrong tree here - is this something that won't work as part of a plugin, and needs to be elsewhere in the code? Or am I just missing something obvious?
Share Improve this question asked Apr 3, 2013 at 22:29 Matt HoweMatt Howe 111 bronze badge1 Answer
Reset to default 1Old Q, but I'll give my 2 cents.
- The walker class should inherit
Walker_Page
, notWalker_Nav_Menu
as is usually the case with guides on the 'net. - The
$item
object is a Post Object, containingpost_title
andID
. To output the URL, you need to callget_permalink($item-ID)
. Regarding$item->url
, it will be unset. - Both
theme_location
andfallback_cb
need to be non-present in the$args
array given towp_nav_menu
.
本文标签: walkerWalkerNavMenu doesn39t work in wppagemenuargs filter
版权声明:本文标题:walker - Walker_Nav_Menu doesn't work in wp_page_menu_args filter 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741960626a2407255.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论