如何隐藏 WP 中除具有当前菜单祖先的项目之外的完整菜单
我有一个 WordPress 网站,我正在尝试解决一个非常简单的问题。我的顶部有一个主菜单,左侧有一个辅助菜单。顶部菜单包含 1 级项目,而左侧菜单包含所有项目。对于左侧菜单,我使用内置的“自定义菜单”小部件。
为了简化外观,我想只显示左侧与当前区域相关的项目,即带有 current-menu-ancestor
的项目。
有什么方法可以通过仅生成 HTML 中所需的代码来优雅地完成此操作吗?要么通过在functions.php中使用一些自定义函数(我正在使用二十一个子主题),要么通过一些“高级自定义菜单”插件(如果有的话)
我正在尝试用 CSS 来做这件事,而且我已经快成功了。我选择了要保留的绿色项目和要隐藏的红色项目,但我无法将 CSS 规则应用于
display: none
红色项目,因为这样它也会隐藏绿色项目。
到目前为止,我的 CSS 是这样的:
#secondary ul {background: red;}
#secondary li.current-menu-ancestor ul,
#secondary li.current-menu-item ul {background: green;}
这是一个示例页面,您可以在其中看到哪些是我试图隐藏的红色项目,哪些是我试图保留的绿色项目。
http://kozossegikertek.hu/csatlakozz/
我希望左侧菜单中仅显示绿色项目。
I have a Wordpress site where I'm trying to solve a very simple problem. I have a primary menu at the top and a secondary menu at the left side. The top menu contains level 1 items while the left menu has all the items. For the left menu I am using the built-in "Custom menu" widget.
To simplify the looks I would like to only show the items on the left which are relevant to the current area, namely items with current-menu-ancestor
.
Is there any way to do it elegantly, by generating only the needed code in HTML? Either by using some custom function in functions.php (I'm using a Twenty Eleven child theme), or by some "Advanced Custom Menu" plugin if there is such?
I am trying to do it in CSS, and I'm almost there. I selected the items green what I like to keep and red what I would like to hide, but I cannot apply a CSS rule to
display: none
the red items, because then it hides the green items too.
My CSS so far is this:
#secondary ul {background: red;}
#secondary li.current-menu-ancestor ul,
#secondary li.current-menu-item ul {background: green;}
Here is a sample page where you can see which are the red items I'm trying to hide and which are the green items I'm trying to keep.
http://kozossegikertek.hu/csatlakozz/
I would like to have only the green items visible on the left menu.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,我做了纯CSS解决方案。如果有人能做到的话,我仍然寻求 WP 特定的解决方案:
OK, I did the pure CSS solution. I'm still up for a WP specific solution if someone can do it:
要通过 CSS 隐藏元素,请使用
display: none;
属性。对于菜单之类的内容,您需要在满足条件(悬停/单击)时使用 Javascript 更改隐藏元素的 css。
To hide an element via CSS, use the
display: none;
property.For something like the menu, you'll want to use Javascript to change the css of the hidden element(s) when a condition is met (hover/click).