昊天SEO

wp_nav_menu()函数控制菜单样式说明

<code>

function wp_nav_menu( $args = array() ) {
static $menu_id_slugs = array();

$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’ => ”,
‘items_wrap’ => ‘<ul id=”%1$s” class=”%2$s”>%3$s</ul>’,
‘depth’ => 0,
‘walker’ => ”,
‘theme_location’ => ” );

$args = wp_parse_args( $args, $defaults );
$args = apply_filters( ‘wp_nav_menu_args’, $args );
$args = (object) $args;

// Get the nav menu based on the requested menu
$menu = wp_get_nav_menu_object( $args->menu );

// Get the nav menu based on the theme_location
if ( ! $menu && $args->theme_location && ( $locations = get_nav_menu_locations() ) && isset( $locations[ $args->theme_location ] ) )
$menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] );

// get the first menu that has items if we still can’t find a menu
if ( ! $menu && !$args->theme_location ) {
$menus = wp_get_nav_menus();
foreach ( $menus as $menu_maybe ) {
if ( $menu_items = wp_get_nav_menu_items($menu_maybe->term_id) ) {
$menu = $menu_maybe;
break;
}
}
}

// If the menu exists, get its items.
if ( $menu && ! is_wp_error($menu) && !isset($menu_items) )
$menu_items = wp_get_nav_menu_items( $menu->term_id );

// If no menu was found or if the menu has no items and no location was requested, call the fallback_cb if it exists
if ( ( !$menu || is_wp_error($menu) || ( isset($menu_items) && emptyempty($menu_items) && !$args->theme_location ) )
&& $args->fallback_cb && is_callable( $args->fallback_cb ) )
return call_user_func( $args->fallback_cb, (array) $args );

// If no fallback function was specified and the menu doesn’t exists, bail.
if ( !$menu || is_wp_error($menu) )
return false;

$nav_menu = $items = ”;

$show_container = false;
if ( $args->container ) {
$allowed_tags = apply_filters( ‘wp_nav_menu_container_allowedtags’, array( ‘div’, ‘nav’ ) );
if ( in_array( $args->container, $allowed_tags ) ) {
$show_container = true;
$class = $args->container_class ? ‘ class=”‘ . esc_attr( $args->container_class ) . ‘”‘ : ‘ class=”menu-’. $menu->slug .’-container”‘;
$id = $args->container_id ? ‘ id=”‘ . esc_attr( $args->container_id ) . ‘”‘ : ”;
$nav_menu .= ‘<’. $args->container . $id . $class . ‘>’;
}
}

// Set up the $menu_item variables
_wp_menu_item_classes_by_context( $menu_items );

$sorted_menu_items = array();
foreach ( (array) $menu_items as $key => $menu_item )
$sorted_menu_items[$menu_item->menu_order] = $menu_item;

unset($menu_items);

$sorted_menu_items = apply_filters( ‘wp_nav_menu_objects’, $sorted_menu_items, $args );

$items .= walk_nav_menu_tree( $sorted_menu_items, $args->depth, $args );
unset($sorted_menu_items);

// Attributes
if ( ! emptyempty( $args->menu_id ) ) {
$wrap_id = $args->menu_id;
} else {
$wrap_id = ‘menu-’ . $menu->slug;
while ( in_array( $wrap_id, $menu_id_slugs ) ) {
if ( preg_match( ‘#-(\d+)$#’, $wrap_id, $matches ) )
$wrap_id = preg_replace(‘#-(\d+)$#’, ‘-’ . ++$matches[1], $wrap_id );
else
$wrap_id = $wrap_id . ‘-1′;
}
}
$menu_id_slugs[] = $wrap_id;

$wrap_class = $args->menu_class ? $args->menu_class : ”;

// Allow plugins to hook into the menu to add their own <li>’s
$items = apply_filters( ‘wp_nav_menu_items’, $items, $args );
$items = apply_filters( “wp_nav_menu_{$menu->slug}_items”, $items, $args );

$nav_menu .= sprintf( $args->items_wrap, esc_attr( $wrap_id ), esc_attr( $wrap_class ), $items );
unset( $items );

if ( $show_container )
$nav_menu .= ‘</’ . $args->container . ‘>’;

$nav_menu = apply_filters( ‘wp_nav_menu’, $nav_menu, $args );

if ( $args->echo )
echo $nav_menu;
else
return $nav_menu;
}

</code>

 

从中发现,该函数可以附带很多参数,因此,我们可以设置div ,ul ,li 标签的class ,id 以及其他很多参数来控制CSS样式,这对于仿站是很必要的一个技巧,所以我把它贴在这里以备参考。

下面是每个参数的说明:

<code>

<?php wp_nav_menu(
array(
‘theme_location’ => ” //指定显示的导航名,如果没有设置,则显示第一个
‘menu’ => ‘header-menu’,
‘container’ => ‘nav’, //最外层容器标签名
‘container_class’ => ‘primary’, //最外层容器class名
‘container_id’ => ”,//最外层容器id值
‘menu_class’ => ‘sf-menu’, //ul标签class
‘menu_id’ => ‘topnav’,//ul标签id
‘echo’ => true,//是否打印,默认是true,如果想将导航的代码作为赋值使用,可设置为false
‘fallback_cb’ => ‘wp_page_menu’,//备用的导航菜单函数,用于没有在后台设置导航时调用
‘before’ => ”,//显示在导航a标签之前
‘after’ => ”,//显示在导航a标签之后
‘link_before’ => ”,//显示在导航链接名之后
‘link_after’ => ”,//显示在导航链接名之前
‘items_wrap’ => ‘<ul id=”%1$s”>%3$s</ul>’,
‘depth’ => 0,////显示的菜单层数,默认0,0是显示所有层
‘walker’ => ”// //调用一个对象定义显示导航菜单 )); ?>

</code

本文地址官网    新乡网站建设,seo

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址