wordpress开发:导航菜单menu.php

常规方式调用

[php]
<?php wp_nav_menu( array( ‘container_class’ => ‘ten columns’,’menu_class’ => ‘main_menu’,’theme_location’ => ‘primary’,’fallback_cb’=> ‘fallbackmenu1’ ) ); ?>
[/php]

 

上面这段代码放在导航栏Div内(下面是调用解释)
这段代码加入到functions.php

[php]
register_nav_menus( array(
‘primary’ => __( ‘Primary Navigation’, ” ),
) );
[/php]

 

最外层容器的标签名,默认div

‘container’ => ‘div’,

最外层容器的class名

‘container_class’ => ‘mainNavBlock’,

最外层容器的id名

‘container_id’ => ‘menu’,

导航菜单ul标签的class名

‘menu_class’ => ‘mainNav’,

导航菜单ul标签的id名

‘menu_id’ => “nav”,

是否打印,默认是true,如果想将导航的代码作为赋值使用,可设置为false

‘echo’ => true,

备用的导航菜单函数,用于没有在后台设置导航时调用

‘fallback_cb’ => ‘the_main_nav’,

显示在导航a标签之前

‘before’ => ‘

显示在导航a标签之后

‘after’ => ‘

显示在导航链接名之前

‘link_before’ => ‘‘,

显示在导航链接名之后

‘link_after’ => ”,

显示的菜单层数,默认0,0是显示所有层

‘depth’ => 0,

调用一个对象定义显示导航菜单

‘walker’ => new Walker_Nav_Menu(),

指定显示的导航名,如果没有设置,则显示第一个

‘theme_location’ => ‘primary’,
);

打印导航菜单

wp_nav_menu( $menu_args );

自定义CSS调用导航菜单 使用GET_TERMS()来获取分类列表

[php]

<?php $args=array( ‘orderby’ => ‘id’,
‘order’ => ‘ASC’
);
$categories=get_categories($args);
foreach($categories as $category) {
echo ‘

<li class="thisclass"><a href="’ . get_category_link( $category->term_id ) . ‘" title="’ . sprintf( __( "View all posts
in %s" ), $category->name ) . ‘" ‘ . ‘>’ . $category->name.'</a></li>

‘;
}
?>

[/php]

 

WordPress自定义菜单实现当前导航高亮

据我的观察,你点击自定义菜单后都会在你点击的那个菜单下加了一个类别选择器,wordpress自己进行了判断,所以我们只要对那个类别选择器写样式即可实现导航的高亮,下面的代码写在style.css上的,当然了按照你的需要来更改吧,代码如下:

[php]
.current-menu-item,.current-menu-parent{
background:#333;
border-radius:4px;
color:#eee;
}
[/php]

 

禁止wp_nav_menu() 输出div和ul标签

[php]
//加入主题中
<?php if(function_exists(‘wp_nav_menu’)) wp_nav_menu(array(‘container’ => false, ‘items_wrap’ => ‘%3$s’, ‘theme_location’ => ‘sidebar_menu’)); ?>

[/php]

 

加入functions.php

[php]
register_nav_menus( array(
‘sidebar_menu’ => ‘内容左边菜单’
) );

[/php]

 

这样输出的就是

[php]

<ul>
<li>…</li>
</ul>

[/php]

 

面包屑导航

[php]
<?php if(function_exists(‘yuyanke_breadcrumbs’)) yuyanke_breadcrumbs();?>
[/php]

 

 

在functions.php加入

[php]
function yuyanke_breadcrumbs() {
$delimiter = ‘»’; // 分隔符
$before = ‘<span class="current">’; // 在当前链接前插入
$after = ‘</span>’; // 在当前链接后插入
if ( !is_home() && !is_front_page() || is_paged() ) {
echo ‘

<div itemscope itemtype="http://schema.org/WebPage" id="crumbs">’.__( ‘您在这里:’ , ‘yuyanke’ );
global $post;
$homeLink = home_url();
echo ‘ <a itemprop="breadcrumb" href="’ . $homeLink . ‘">’ . __( ‘首页’ , ‘yuyanke’ ) . ‘</a> ‘ . $delimiter . ‘ ‘;
if ( is_category() ) { // 分类 存档
global $wp_query;
$cat_obj = $wp_query->get_queried_object();
$thisCat = $cat_obj->term_id;
$thisCat = get_category($thisCat);
$parentCat = get_category($thisCat->parent);
if ($thisCat->parent != 0){
$cat_code = get_category_parents($parentCat, TRUE, ‘ ‘ . $delimiter . ‘ ‘);
echo $cat_code = str_replace (‘<a’,'<a itemprop="breadcrumb"’, $cat_code );
}
echo $before . ” . single_cat_title(”, false) . ” . $after;
} elseif ( is_day() ) { // 天 存档
echo ‘<a itemprop="breadcrumb" href="’ . get_year_link(get_the_time(‘Y’)) . ‘">’ . get_the_time(‘Y’) . ‘</a> ‘ . $delimiter . ‘ ‘;
echo ‘<a itemprop="breadcrumb" href="’ . get_month_link(get_the_time(‘Y’),get_the_time(‘m’)) . ‘">’ . get_the_time(‘F’) . ‘</a> ‘ . $delimiter . ‘ ‘;
echo $before . get_the_time(‘d’) . $after;
} elseif ( is_month() ) { // 月 存档
echo ‘<a itemprop="breadcrumb" href="’ . get_year_link(get_the_time(‘Y’)) . ‘">’ . get_the_time(‘Y’) . ‘</a> ‘ . $delimiter . ‘ ‘;
echo $before . get_the_time(‘F’) . $after;
} elseif ( is_year() ) { // 年 存档
echo $before . get_the_time(‘Y’) . $after;
} elseif ( is_single() && !is_attachment() ) { // 文章
if ( get_post_type() != ‘post’ ) { // 自定义文章类型
$post_type = get_post_type_object(get_post_type());
$slug = $post_type->rewrite;
echo ‘<a itemprop="breadcrumb" href="’ . $homeLink . ‘/’ . $slug[‘slug’] . ‘/">’ . $post_type->labels->singular_name . ‘</a> ‘ . $delimiter . ‘ ‘;
echo $before . get_the_title() . $after;
} else { // 文章 post
$cat = get_the_category(); $cat = $cat[0];
$cat_code = get_category_parents($cat, TRUE, ‘ ‘ . $delimiter . ‘ ‘);
echo $cat_code = str_replace (‘<a’,'<a itemprop="breadcrumb"’, $cat_code ); echo $before . get_the_title() . $after; } } elseif ( !is_single() && !is_page() && get_post_type() != ‘post’ ) { $post_type = get_post_type_object(get_post_type()); echo $before . $post_type->labels->singular_name . $after;
} elseif ( is_attachment() ) { // 附件
$parent = get_post($post->post_parent);
$cat = get_the_category($parent->ID); $cat = $cat[0];
echo ‘<a itemprop="breadcrumb" href="’ . get_permalink($parent) . ‘">’ . $parent->post_title . ‘</a> ‘ . $delimiter . ‘ ‘;
echo $before . get_the_title() . $after;
} elseif ( is_page() && !$post->post_parent ) { // 页面
echo $before . get_the_title() . $after;
} elseif ( is_page() && $post->post_parent ) { // 父级页面
$parent_id = $post->post_parent;
$breadcrumbs = array();
while ($parent_id) {
$page = get_page($parent_id);
$breadcrumbs[] = ‘<a itemprop="breadcrumb" href="’ . get_permalink($page->ID) . ‘">’ . get_the_title($page->ID) . ‘</a>’;
$parent_id = $page->post_parent;
}
$breadcrumbs = array_reverse($breadcrumbs);
foreach ($breadcrumbs as $crumb) echo $crumb . ‘ ‘ . $delimiter . ‘ ‘;
echo $before . get_the_title() . $after;
} elseif ( is_search() ) { // 搜索结果
echo $before ;
printf( __( ‘Search Results for: %s’, ‘taiji’ ), get_search_query() );
echo $after;
} elseif ( is_tag() ) { //标签 存档
echo $before ;
printf( __( ‘Tag Archives: %s’, ‘taiji’ ), single_tag_title( ”, false ) );
echo $after;
} elseif ( is_author() ) { // 作者存档
global $author;
$userdata = get_userdata($author);
echo $before ;
printf( __( ‘Author Archives: %s’, ‘taiji’ ), $userdata->display_name );
echo $after;
} elseif ( is_404() ) { // 404 页面
echo $before;
_e( ‘Not Found’, ‘taiji’ );
echo $after;
}
if ( get_query_var(‘paged’) ) { // 分页
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() )
echo sprintf( __( ‘( Page %s )’, ‘taiji’ ), get_query_var(‘paged’) );
}
echo ‘</div>

‘;
}
}

[/php]

 

发表评论