选择父类别时在侧边栏中显示子类别
我试图在 WP 3.1.x 中使用 wp_nav_menu
函数实现的目标是拥有一个菜单,例如:
-- Category 1
-- Category 2
-- Category 3
-- Category 4
-- Category 5
-- Category 6
但是当您位于“类别 2”时,它显示为:
-- Category 1
-- Category 2
-- Sub Category 1
-- Sub Category 2
-- Sub Category 3
-- Category 3
-- Category 4
-- Category 5
-- Category 6
PS。我不是在寻找 CSS 隐藏/显示功能,这可以轻松实现。
聚苯硫醚。扩展 Walker_Nav_Menu
类是否有可能的解决方案?
What I'm trying to achieve in WP 3.1.x using the wp_nav_menu
function, is to have a menu such as:
-- Category 1
-- Category 2
-- Category 3
-- Category 4
-- Category 5
-- Category 6
But when you're on 'Category 2' it displays as:
-- Category 1
-- Category 2
-- Sub Category 1
-- Sub Category 2
-- Sub Category 3
-- Category 3
-- Category 4
-- Category 5
-- Category 6
PS. I'm not looking for CSS hide/show functionality, that can be achieved easily.
PPS. Is a possible solution for this extending the Walker_Nav_Menu
class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您对
Walker_Nav_Menu
类进行子类化,则可以重写display_element
函数,并在循环之前有条件地删除项目的子项。这将删除任何非当前项目的子项,因此如果您只想对类别执行此操作,则需要检查项目的类型。完整的步行器看起来像这样: https://gist.github.com/954627
If you subclass the
Walker_Nav_Menu
class you can override thedisplay_element
function and conditionally remove an item's children before they are looped through.This would remove the children of any item that is not current, so you would need to check the item's type if you just want to do this for categories. The complete walker would look something like this: https://gist.github.com/954627