自得怡乐 發表於 2023-9-7 00:00:00

WordPress 3.0+菜单功能支持二级和N级菜单实现步骤

<p>
<span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>自带的导航菜单功能是 WordPress 3.0+ 中唯一执得让人眼睛一亮的功能了,在新做主题的过程中再一次让我眼睛亮了一亮,发现这个 WordPress 的导航菜单居然还支持二级和N级菜单。 </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>当然要实现下拉的二级菜单功能,还是需要 jQuery 和 CSS 等的配合。下面简单介绍一下: </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>首先,要让你的主题支持 WordPress 3.0+,只需简单几步,把以下代码添加到主题的functions.php中: </span></p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<p style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span></p>
<p>
代码如下:</p>
</div>
<p style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
&lt;?php<br>
if ( function_exists('register_nav_menus') ) {<br>
register_nav_menus(array(<br>
'primary' =&gt; '导航菜单'<br>
));<br>
}<br>
?&gt;</p>
<p>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>然后,在 header.php 中调用如下代码: </span></p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<p style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span></p>
<p>
代码如下:</p>
</div>
<p style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
&lt;?php<br>
if(function_exists('wp_nav_menu')) {<br>
wp_nav_menu(array('theme_location'=&gt;'primary','menu_id'=&gt;'nav','container'=&gt;'ul'));<br>
}<br>
?&gt;</p>
<p>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>接下来,就可以在 WordPress 后台控制面板的菜单中看到菜单选项了,只要拖动模块至一级菜单下就会形成二级菜单,甚至三级菜单,就像拖动小工具里的模块一样操作方便。如图: </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><img title="WordPress 3.0+菜单功能支持二级和N级菜单实现步骤" alt="WordPress 3.0+菜单功能支持二级和N级菜单实现步骤" src="https://zhuji.jb51.net/uploads/img/202305/21cc04bca115dcb22f29c67848600505.jpg" style="max-width:100%!important;height:auto!important;border: 1px solid rgb(204, 204, 204); vertical-align: middle; padding: 1px; overflow: hidden; max-width: 696px; font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px; width: 500px; height: 590px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><strong style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>注意</strong><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>:看到上图的红框没有,可以拖动的,前面空一个空就是二级菜单,空2空就能出三级菜单,类推,是不是很简单哦,O(∩_∩)O~ </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>根据以上操作步骤,生成相应的代码:</span></p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<p style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span></p>
<p>
代码如下:</p>
</div>
<p style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
&lt;ul id="nav"&gt;<br>
&lt;li id="menu-item-7"&gt;&lt;a href="DUMMY"&gt;Fiber Fusion Splicer&lt;/a&gt;<br>
&lt;ul&gt;<br>
&lt;li id="menu-item-8"&gt;&lt;a href="DUMMY"&gt;EG6871&lt;/a&gt;&lt;/li&gt;<br>
&lt;li id="menu-item-9"&gt;&lt;a href="DUMMY"&gt;EG6871A&lt;/a&gt;&lt;/li&gt;<br>
&lt;li id="menu-item-10"&gt;&lt;a href="DUMMY"&gt;EG6872&lt;/a&gt;&lt;/li&gt;<br>
&lt;/ul&gt;<br>
&lt;/li&gt;<br>
&lt;li id="menu-item-11"&gt;&lt;a href="DUMMY"&gt;Light Source&lt;/a&gt;<br>
&lt;ul&gt;<br>
&lt;li id="menu-item-12"&gt;&lt;a href="DUMMY"&gt;EG-OLS-18&lt;/a&gt;&lt;/li&gt;<br>
&lt;li id="menu-item-13"&gt;&lt;a href="DUMMY"&gt;EG-OLS-18V&lt;/a&gt;&lt;/li&gt;<br>
&lt;li id="menu-item-14"&gt;&lt;a href="DUMMY"&gt;EG-OLS-19&lt;/a&gt;&lt;/li&gt;<br>
&lt;li id="menu-item-15"&gt;&lt;a href="DUMMY"&gt;EG-OLS-19V&lt;/a&gt;&lt;/li&gt;<br>
&lt;/ul&gt;<br>
&lt;/li&gt;<br>
&lt;li id="menu-item-16"&gt;&lt;a href="DUMMY"&gt;Optica Cable&lt;/a&gt;<br>
&lt;ul&gt;<br>
&lt;li id="menu-item-17"&gt;&lt;a href="DUMMY"&gt;GYFTY&lt;/a&gt;&lt;/li&gt;<br>
&lt;li id="menu-item-18"&gt;&lt;a href="DUMMY"&gt;GYTA&lt;/a&gt;&lt;/li&gt;<br>
&lt;/ul&gt;<br>
&lt;/li&gt;<br>
&lt;/ul&gt;</p>
<p>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>一个二级菜单的 的模型已经展现出来了,剩下的就是配上 CSS 和 jQuery,使其产生下拉菜单的效果。 </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>执行的脚本也非常简单,如下: </span></p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<p style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span></p>
<p>
代码如下:</p>
</div>
<p style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
&lt;script type="text/javascript"&gt;<br>
jQuery(document).ready(function($) {<br>
$('#nav li').hover(function() {<br>
$('ul', this).slideDown(300)<br>
},<br>
function() {<br>
$('ul', this).slideUp(300)<br>
});<br>
});<br>
&lt;/script&gt;</p>
<p>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>然后加上相应的css样式:</span></p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<p style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span></p>
<p>
代码如下:</p>
</div>
<p style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
&lt;style type="text/css"&gt;<br>
ul,li{padding:0;margin:0;list-style:none;}<br>
a{text-decoration:none;}<br>
#nav li{width:100px;line-height:30px;float:left;}<br>
#nav li a{text-align:center;display:block;width:100px;background:#ccc;}<br>
#nav li a:hover{background:#000;color:#fff;}<br>
.sub-menu{display:none;}<br>
&lt;/style&gt;</p>
<p>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>大功告成,上面样式是一些基本的东西,你可以根据自己的主题来设计。 </span></p>
頁: [1]
查看完整版本: WordPress 3.0+菜单功能支持二级和N级菜单实现步骤