wordpress 在主题中使用菜单

在Wordpress的管理页面,点击外观中的菜单,居然发现我这主题不支持菜单,提示如下

Your theme does not natively support menus

查了下需要使用到 register_nav_menus 函数和wp_nav_menu函数

使用步骤:

 

1.在主题文件夹的functions.php中加入注册代码

 

if ( function_exists(‘register_nav_menus’))
register_nav_menus();

 

2.在你需要用的地方放上wp_nav_menu函数

 

我网站中使用的例子:

<?php
wp_nav_menu( array(‘items_wrap’=> ‘<ul id=”nav” >%3$s</ul>’) );
?>

 

3.wp_nav_menu函数参数

 

这函数的参数是一个数组,里面可以定义非常多的东西,比如:

  1. container,就是包裹的html标签
  2. container_class,包裹html标签的class
  3. container_id,包裹html标签的id

还有非常多就不列出来了。

大家可以参考:http://codex.wordpress.org/Function_Reference/wp_nav_menu

<?php

$defaults = array(
	'theme_location'  => '',
	'menu'            => '',
	'container'       => 'div',
	'container_class' => '',
	'container_id'    => '',
	'menu_class'      => 'menu',
	'menu_id'         => '',
	'echo'            => true,
	'fallback_cb'     => 'wp_page_menu',
	'before'          => '',
	'after'           => '',
	'link_before'     => '',
	'link_after'      => '',
	'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>',
	'depth'           => 0,
	'walker'          => ''
);

wp_nav_menu( $defaults );

?>

文章源地址:http://www.waitingfy.com/?p=678

Tags:

678

Leave a Reply

Name and Email Address are required fields.
Your email will not be published or shared with third parties.