WordPress3.0 的自定义菜单功能

升级到wordpress3.0之后,会发现多了一些功能,其中一个是在 “外观”下多了个 Menus 选项。点开后你会发现一个提示

“The current theme does not natively support menus, but you can use the “Custom Menu” widget to add any menus you create here to the theme’s sidebar.”

意思是这个主题不支持这个功能,但是你可以在 小工具里使用“Custom Menu”在 sidebar 里实现。

通过 register_nav_menus( $locations = array() ); 可以激活该功能。

例如

if ( function_exists( 'register_nav_menus' ) ) {
	register_nav_menu(
		array(
		  'location_1' => 'The name of Location',
		  'location_2' => 'the name of location'
		)
	);
}
这样可以在这儿位置创建2个自定义菜单的选择框。

新的菜单功能是通过 wp_nav_menu() 函数实现的,所以如果你的主题里没有这个函数,就会出现上面那段提示。

OK,下面详细介绍一下这个函数。

描述

显示一个导航菜单。菜单可以在后台建立: Appearance > Menus。

使用

<?php wp_nav_menu($args); ?>

默认设置

<?php $defaults = array(
'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' => ,
'depth'           => 0,
'walker'          => ,
'theme_location' => );
?>

可选参数

$menu
(string) (可选) 期望显示的菜单The menu that is desired; accepts (matching in order) id, slug, name

默认: None
$container
(string) (可选) 容器,可以设置为 ul 或 div。Whether to wrap the ul, and what to wrap it with

默认: div
$container_class
(string) (可选) 容器的 class 。the class that is applied to the container

默认: menu-{menu slug}-container
$container_id
(string) (可选) 容器的 id 。The ID that is applied to the container

默认: None
$menu_class
(string) (可选)菜单的 ul 元素的 class。CSS class to use for the ul element which forms the menu

默认: menu
$menu_id
(string) (可选) 菜单 ul 元素的 id。The ID that is applied to the ul element which forms the menu

默认: menu slug, incremented
$echo
(boolean) (可选) Whether to echo the menu or return it

默认: true
$fallback_cb
(string) (可选)如果惨不存在使用的返回函数。 If the menu doesn’t exists, the callback function to use

默认: wp_page_menu
$before
(string) (可选) Output text before the link text

默认: None
$after
(string) (可选) Output text after the link text

默认: None
$link_before
(string) (可选) Output text before the link

默认: None
$link_after
(string) (可选) Output text after the link

默认: None
$depth
(integer) (可选) 可以设置成几级菜单,0 为无限极。 how many levels of the hierarchy are to be included where 0 means all

默认: 0
$walker
(object) (可选) Custom walker object to use

默认: None
$theme_location
(string) (可选)指定菜单的位置。位置必须是 register_nav_menu() 里定义的。 the location in the theme to be used–must be registered with register_nav_menu() in order to be selectable by the user

默认: None

示例

默认

<div class="access">
    <?php wp_nav_menu(); ?>
</div>

显示一个指定的菜单

<?php wp_nav_menu( array('menu' => 'Project Nav' )); ?>

Twenty Ten 主题里的用法

<div id="access" role="navigation">
<?php /*  Allow screen readers / text browsers to skip the navigation menu and get right to the good stuff */ ?>
<div class="skip-link screen-reader-text"><a href="#content" title="<?php esc_attr_e( 'Skip to content', 'twentyten' ); ?>">
<?php _e( 'Skip to content', 'twentyten' ); ?></a></div>
<?php /* Our navigation menu.  If one isn't filled out, wp_nav_menu falls back to wp_page_menu.
The menu assiged to the primary position is the one used.  If none is assigned, the menu with the lowest ID is used.
我们的导航菜单,如果为空,wp_nav_menu 返回执行 wp_page_menu。
*/ ?>
<?php wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary' ) ); ?>
</div><!-- #access -->

Removing the Navigation Container

<?php
function my_wp_nav_menu_args( $args = ” )
{
$args[‘container’] = false;
return $args;
} // function

add_filter( ‘wp_nav_menu_args’, ‘my_wp_nav_menu_args’ );
?>