如何删除WordPress菜单中的class或id样式?
WordPress对于PHP程序员已经再熟悉不过了。截至2019年08月27日,来自于W3Techs的最新数据,WordPress被34.5%的网站使用,43.6%的网站不使用没有使用内容管理系统,在内容管理系统市场WordPress占有率为61.2%。WordPress的wp-nav-menu函数主要是来生成菜单的,默认会有很多多余的class和id。今天雷雪松就给大家介绍一下如何删除WordPress菜单中的class或id样式。
以雷雪松的博客为例,默认生成的菜单源代码如下:
1 2 3 4 5 6 7 8 9 | <ul> <li id="menu-item-15" class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home menu-item-15"><a title="雷雪松的博客-优秀个人技术博客" href="https://www.xuesongboke.cn" aria-current="page"><strong>首页</strong></a></li> <li id="menu-item-26" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-26"><a title="Linux" href="https://www.xuesongboke.cn/category/linux"><strong>Linux</strong></a></li> <li id="menu-item-29" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-29"><a title="PHP" href="https://www.xuesongboke.cn/category/php"><strong>PHP</strong></a></li> <li id="menu-item-30" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-30"><a title="WEB前端" href="https://www.xuesongboke.cn/category/web"><strong>WEB前端</strong></a></li> <li id="menu-item-27" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-27"><a title="MySQL" href="https://www.xuesongboke.cn/category/mysql"><strong>MySQL</strong></a></li> <li id="menu-item-28" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-28"><a title="NoSQL" href="https://www.xuesongboke.cn/category/nosql"><strong>NoSQL</strong></a></li> <li id="menu-item-2023" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-2023"><a title="人生杂谈" href="https://www.xuesongboke.cn/category/life"><strong>人生杂谈</strong></a></li> </ul> |
期望生成的源代码:
1 2 3 4 5 6 7 8 9 | <ul> <li><a title="雷雪松的博客-优秀个人技术博客" href="https://www.xuesongboke.cn" aria-current="page"><strong>首页</strong></a></li> <li><a title="Linux" href="https://www.xuesongboke.cn/category/linux"><strong>Linux</strong></a></li> <li><a title="PHP" href="https://www.xuesongboke.cn/category/php"><strong>PHP</strong></a></li> <li><a title="WEB前端" href="https://www.xuesongboke.cn/category/web"><strong>WEB前端</strong></a></li> <li><a title="MySQL" href="https://www.xuesongboke.cn/category/mysql"><strong>MySQL</strong></a></li> <li><a title="NoSQL" href="https://www.xuesongboke.cn/category/nosql"><strong>NoSQL</strong></a></li> <li><a title="人生杂谈" href="https://www.xuesongboke.cn/category/life"><strong>人生杂谈</strong></a></li> </ul> |
如何删除WordPress菜单中的class或id样式?实现的方式也很简单,在主题的 functions.php增加过滤器钩子函数add_filter()。
1 2 3 4 5 6 7 | //删除WordPress菜单中的class或id样式 add_filter('nav_menu_css_class', 'delete_css_attributes_filter', 100, 1); add_filter('nav_menu_item_id', 'delete_css_attributes_filter', 100, 1); add_filter('page_css_class', 'delete_css_attributes_filter', 100, 1); function delete_css_attributes_filter($var) { return is_array($var) ? array() : ''; } |
作为程序员已经很辛苦了, 借助别人的经验能少写代码就少写代码,能少思考就少思考,不要再自己一点点研究找问题了。比如如何删除WordPress菜单中的class或id样式这个问题。如果从熟悉源码自己一点点研究可能需要一两天,但是如果直接在网上找可能五分钟就可以达到目的。有时候,学习很重要,方法和效率也不容忽视。当然有些基础的东西还是要掌握的,如果网上也没有别人的经验,那就的自己做了。
2019年8月27日 下午2:19
WordPress感觉还是有点慢,我用的typeecho就觉得快很多