博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
wordpress 菜单_如何整理WordPress菜单HTML
阅读量:2507 次
发布时间:2019-05-11

本文共 4255 字,大约阅读时间需要 14 分钟。

wordpress 菜单

I love WordPress. I also love clean semantic HTML. Unfortunately, several of the standard WordPress theme functions return code that is a little untidy. For me, the primary culprits are and the newer ; both return an unordered list of page links typically used for page menus and sitemaps, e.g.,

我喜欢WordPress。 我也喜欢干净的语义HTML。 不幸的是,一些标准的WordPress主题函数返回的代码有点混乱。 对我而言,罪魁祸首是和较新的 ; 都返回通常用于页面菜单和站点地图的页面链接的无序列表,例如,

2 )); ?>

The code results in this HTML abomination for the default installation’s home, about and contact pages:

该代码导致默认安装主页面,关于页面和联系页面HTML可憎:

The code is valid but it contains items we generally don’t need:

该代码有效,但其中包含我们通常不需要的项目:

  • Strictly speaking, the outer div isn’t required. I’d prefer either to give the ul an ID such as “navigation” or use the HTML5 nav element.

    严格来说,不需要外部div 。 我更愿意给ul一个ID,例如“导航”,或者使用HTML5 nav元素。

  • We don’t need a title attribute when our link contains identical text.

    当我们的链接包含相同的文本时,我们不需要title属性。
  • Does our CSS or JavaScript require hooks for “page_item” and “page-item-N” classes?

    我们CSS或JavaScript是否需要对“ page_item”和“ page-item-N”类进行钩子?
  • The “children” class for the sub-links list isn’t necessary — we can style them using a selector such as “nav ul ul li.”

    子链接列表的“儿童”类不是必需的-我们可以使用“ nav ul ul li”之类的选择器来设置其样式。
  • The current_page_ancestor and current_page_parent classes mean the same thing, but I’d prefer a single shorter name such as “open.”

    current_page_ancestor和current_page_parent类具有相同的含义,但我希望使用一个较短的名称,例如“ open”。
  • Similarly, I want rename current_page_item to “active.”

    同样,我想将current_page_item重命名为“ active”。
  • Do we require the full page URLs — we could use shorter absolute addresses such as /, /about and /contact?

    我们是否需要完整的页面URL —我们可以使用较短的绝对地址,例如/,/ about和/ contact?

There are several ways to tidy the HTML, but the simplest solution replaces strings using regular expressions.

有几种整理HTML的方法,但是最简单的解决方案是使用正则表达式替换字符串。

note: The WordPress 3 Walker object In WordPress 3.0, a custom Walker object can be passed as an argument to wp_nav_menu(). The object provides code to output your own custom HTML for every page link. While this will be useful in some circumstances, you’ll possibly require regexs for the outer HTML, the code won’t necessarily be shorter, and it won’t work in WordPress 2.x and below.

注意: WordPress 3 Walker对象在WordPress 3.0中,可以将自定义Walker对象作为参数传递给wp_nav_menu()。 该对象提供代码以为每个页面链接输出您自己的自定义HTML。 尽管在某些情况下这很有用,但您可能需要外部HTML的正则表达式,代码不一定会更短,并且在WordPress 2.x及以下版本中将无法使用。

Here’s the PHP code to output a tidier HTML menu to 2 levels (main menu and sub-menu). In most cases, it should replace the call to wp_nav_menu() or wp_list_pages() in your theme’s header.php file:

这是PHP代码,可将HTML菜单输出到2个级别(主菜单和子菜单)。 在大多数情况下,它应该替换主题主题的header.php文件中对wp_nav_menu()或wp_list_pages()的调用:

echo preg_replace(array(    '/t/', // remove tabs    '/'.str_replace('//','//', get_bloginfo('url')).'/i', // remove full URL    '/current_page_items*/i',    '/current_page_ancestors*/i',    '/current_page_parents*/i',    '/page_items+/i',    '/page-item-d+s*/i',    '/childrens*/i',    '/s*class=["']["']/i', // empty classes    '/s*title="[^"]+"/i', // all titles    '/s+>/i',    '/div>/i' // change div to nav  ),  array(    '',    '',    'active',    'open',    '',    '',    '',    '',    '',    '',    '>',    'nav>'  ),  wp_nav_menu(array( 'menu_class'=>'', 'depth'=>2, 'echo'=>false )));

If you’re using a version of WordPress prior to version 3, replace the penultimate “wp_nav_menu(…)” line with:

如果您使用的是版本3之前的WordPress版本,则将倒数第二个“ wp_nav_menu(…)”行替换为:

"
"

Our resulting HTML is much cleaner and has been reduced by more than 50%. Longer menus may result in larger savings.

我们生成HTML更加干净,减少了50%以上。 较长的菜单可以节省更多的钱。

Please note that regular expressions are powerful but dangerous. You may need to change the code if you’re using a deeper page depth or have a page named “children” or “page_item.”

请注意,正则表达式功能强大但危险。 如果您使用更深的页面深度或具有名为“ children”或“ page_item”的页面,则可能需要更改代码。

There’s no excuse now — go and tidy your WordPress HTML!

现在没有任何借口–整理一下WordPress HTML!

翻译自:

wordpress 菜单

转载地址:http://xdrgb.baihongyu.com/

你可能感兴趣的文章
Linux(SUSE 12)安装jboss4并实现远程访问
查看>>
Neutron在给虚拟机分配网络时,底层是如何实现的?
查看>>
netfilter/iptables全攻略
查看>>
Overlay之VXLAN架构
查看>>
Eclipse : An error occurred while filtering resources(Maven错误提示)
查看>>
在eclipse上用tomcat部署项目404解决方案
查看>>
web.xml 配置中classpath: 与classpath*:的区别
查看>>
suse如何修改ssh端口为2222?
查看>>
详细理解“>/dev/null 2>&1”
查看>>
suse如何创建定时任务?
查看>>
suse搭建ftp服务器方法
查看>>
centos虚拟机设置共享文件夹并通过我的电脑访问[增加smbd端口修改]
查看>>
文件拷贝(IFileOperation::CopyItem)
查看>>
MapReduce的 Speculative Execution机制
查看>>
大数据学习之路------借助HDP SANDBOX开始学习
查看>>
Hadoop基础学习:基于Hortonworks HDP
查看>>
为什么linux安装程序 都要放到/usr/local目录下
查看>>
Hive安装前扫盲之Derby和Metastore
查看>>
永久修改PATH环境变量的几种办法
查看>>
大数据学习之HDP SANDBOX开始学习
查看>>