如何制作一个WordPress主题的下拉式菜单?

导语:最近要制作一个WordPress,需要用到下拉式菜单,说句实话,以前的CSS代码都是前端写好的,基本上不用自己来修改,但这次没法找到前端,所以有点抓狂,因为不知道怎么写,在网上找了一下资料,终于会了。

WordPress的菜单函数

在WordPress中一般要先注册菜单,注册代码一般放在functions.php中,如果你放在其他的php里面也是可以的,只要最后加载了就行,函数的使用方法我这里就不多说了,自行搜索文档就可以了。

register_nav_menus( array(
	'primary' => __( 'Primary Menu', 'twentysixteen' ),
	'social'  => __( 'Social Links Menu', 'twentysixteen' ),
	'tmsn'  => __( 'Name Menu', 'twentysixteen' ),
	) );

注册菜单后,在特定的位置将结果输出出来,在你希望输出的位置放置下面的代码,wp_nav_menu()这个函数有很多参数,你可以去查看详细的信息,自行搜索。

wp_nav_menu( array('theme_location'=> 'tmsn',
	'container'       => 'div', //最外层容器标签名
	'container_class' => 'nav-menu', //最外层容器class名
	'container_id'    => 'tmsn-menu-div',//最外层容器id值
	'menu_class'      => 'nav', //ul标签class
	'menu_id'         => 'tmsn-menu-ul'//ul标签id
	));

WordPress的菜单HTML代码

下面的代码就是一个示例,ul li 标签里面再嵌套一个ul li 标签,这个嵌套的ul标签有一个class:sub-menu,意味着这个是一个二级菜单。这时,菜单是乱七八糟排列的,这时需要用到CSS来控制。

<ul id="tmsn-menu-ul" class="nav">
	<li id="menu-item-84" class="menu-item menu-item-type-custom menu-item-object-custom current-menu-ancestor current-menu-parent menu-item-has-children menu-item-84">
		<a title="Popular" href="#">Popular</a>
		<ul class="sub-menu">
			<li id="menu-item-93" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-93">
			<a href="#">Popular</a>
			</li>
		</ul>
	</li>
</ul>

WordPress的菜单CSS代码

首先,在网页加载的时候,二级菜单应该是隐藏的,不显示的,那么在CSS里面控制,位置为绝对位置。

.nav ul {
    position: absolute;
    display: none;
    top: 100%;
    right: 0px;
    z-index: 80;
    background: #eee;
}

当我们的鼠标指向菜单Popular时,它的二级菜单就应该显示出来了,也是由CSS来控制。

.nav li:hover > ul {
	display: block;
}

大于号 “> ” 为CSS子元素选择器,表示选择作为 li 元素子元素的所有 ul 元素,将它显示出来。这样,当你的鼠标移动到菜单Popular上时,二级菜单就显示出来了。

结束

你学会了吗?开始的时候,叶子还以为要使用js才能制作菜单,没有想到,现在css就能做出好看的下拉菜单效果了。


Warning: copy(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /opt/lampp/htdocs/wordpresssummer/wp-content/themes/summerleaf/inc/summer_setting.php on line 546

Warning: copy(): Failed to enable crypto in /opt/lampp/htdocs/wordpresssummer/wp-content/themes/summerleaf/inc/summer_setting.php on line 546

Warning: copy(https://www.wordpressleaf.com/wp-content/themes/summerleaf/images/tx/35.png): failed to open stream: operation failed in /opt/lampp/htdocs/wordpresssummer/wp-content/themes/summerleaf/inc/summer_setting.php on line 546

目前在“如何制作一个WordPress主题的下拉式菜单?”上有2条评论


  1. Warning: copy(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /opt/lampp/htdocs/wordpresssummer/wp-content/themes/summerleaf/inc/summer_setting.php on line 546

    Warning: copy(): Failed to enable crypto in /opt/lampp/htdocs/wordpresssummer/wp-content/themes/summerleaf/inc/summer_setting.php on line 546

    Warning: copy(https://www.wordpressleaf.com/wp-content/themes/summerleaf/images/tx/5.png): failed to open stream: operation failed in /opt/lampp/htdocs/wordpresssummer/wp-content/themes/summerleaf/inc/summer_setting.php on line 546
    Javen说道:

    前来学习。


    1. Warning: copy(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /opt/lampp/htdocs/wordpresssummer/wp-content/themes/summerleaf/inc/summer_setting.php on line 546

      Warning: copy(): Failed to enable crypto in /opt/lampp/htdocs/wordpresssummer/wp-content/themes/summerleaf/inc/summer_setting.php on line 546

      Warning: copy(https://www.wordpressleaf.com/wp-content/themes/summerleaf/images/tx/89.png): failed to open stream: operation failed in /opt/lampp/htdocs/wordpresssummer/wp-content/themes/summerleaf/inc/summer_setting.php on line 546
      编辑 杨说道:

      共同进步。

发表评论

邮箱地址不会被公开。 必填项已用*标注