加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > CMS系统 > wordpress > 正文

WordPress 添加面包屑导航的二种方法

发布时间:2020-12-14 14:28:40 所属栏目:wordpress 来源:网络整理
导读:由站长 通过网络收集整理的代码片段。编程之家小编现在分享给大家,也给大家做个参考。 面包屑导航,简单的说它就是提供给用户回溯到网站首页或入口页面的一条快速路径。今天创客分享一下 wordpress 添加面包屑导航的三种方法,希望对大家有

以下代码由PHP站长网 52php.cn收集自互联网现在PHP站长网小编把它分享给大家,仅供参考

面包屑导航,简单的说它就是提供给用户回溯到网站首页或入口页面的一条快速路径。今天创客分享一下 wordpress 添加面包屑导航的三种方法,希望对大家有所帮助,原文参考自园子博客。

面包屑定义:

面包屑通常出现在页面顶部,一般会位于标题或页头的下方。它提供给用户返回之前任何一个页面的链接(这些链接也是能到达当前页面的路径),在层级架构中通常是这个页面的父级页面。也可以这样理解,面包屑提供给用户回溯到网站首页或入口页面的一条快速路径,它们绝大部分看起来就像这样:首页→分类页→次级分类页。如下图所示:


面包屑好处:

1.可以提供多路径的交互方式,方便用户跳转到其它页面。在页面及分类多的网站中尤其有用。

2.面包屑导航信息结构对于网站的 SEO 也有着大的好处,它可以更多的强调网站关键字,扩大关键字的范围,从而达到更好的优化目的。

3.它从一个侧面展示了该信息集合的信息结构和集合方式,可以让用户在最快的时间之内找到需要的东西。

方案一:

把以下代码直接添加到你想出现面包屑导航的位置,比如 header.php 里面,也可以放在 single.php 页面的导航标题上面,你有可能需要添加的页面可能有:archive.php、archives.php、links.php、page.php。

<div class="mbx-dh">

当前位置:<a href="<?php bloginfo('url'); ?>"><?php bloginfo('name'); ?></a> &raquo;

<?php

if( is_single() ){

$categorys = get_the_category();

$category = $categorys[0];

echo( get_category_parents($category->term_id,true,' &raquo; ') );

the_title();

} elseif ( is_page() ){

the_title();

} elseif ( is_category() ){

single_cat_title();

} elseif ( is_tag() ){

single_tag_title();

} elseif ( is_day() ){

the_time('Y年Fj日');

} elseif ( is_month() ){

the_time('Y年F');

} elseif ( is_year() ){

the_time('Y年');

} elseif ( is_search() ){

echo $s.' 的搜索结果';

}

?>

</div>

方案二:

首先把以下代码添加到主题的 functions.php 文件中

function dimox_breadcrumbs() {

?

$delimiter = '&raquo;';

$name = 'Home'; //text for the 'Home' link

$currentBefore = '<span>';

$currentAfter = '</span>';

?

if ( !is_home() && !is_front_page() || is_paged() ) {

?

echo '<div id="crumbs">';

?

global $post;

$home = get_bloginfo('url');

echo '' . $name . ' ' . $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) echo(get_category_parents($parentCat,TRUE,' ' . $delimiter . ' '));

echo $currentBefore . 'Archive by category &#39;';

single_cat_title();

echo '&#39;' . $currentAfter;

?

} elseif ( is_day() ) {

echo '' . get_the_time('Y') . ' ' . $delimiter . ' ';

echo '' . get_the_time('F') . ' ' . $delimiter . ' ';

echo $currentBefore . get_the_time('d') . $currentAfter;

?

} elseif ( is_month() ) {

echo '' . get_the_time('Y') . ' ' . $delimiter . ' ';

echo $currentBefore . get_the_time('F') . $currentAfter;

?

} elseif ( is_year() ) {

echo $currentBefore . get_the_time('Y') . $currentAfter;

?

} elseif ( is_single() ) {

$cat = get_the_category(); $cat = $cat[0];

echo get_category_parents($cat,' ' . $delimiter . ' ');

echo $currentBefore;

the_title();

echo $currentAfter;

?

} elseif ( is_page() && !$post->post_parent ) {

echo $currentBefore;

the_title();

echo $currentAfter;

?

} elseif ( is_page() && $post->post_parent ) {

$parent_id = $post->post_parent;

$breadcrumbs = array();

while ($parent_id) {

$page = get_page($parent_id);

$breadcrumbs[] = '' . get_the_title($page->ID) . '';

$parent_id = $page->post_parent;

}

$breadcrumbs = array_reverse($breadcrumbs);

foreach ($breadcrumbs as $crumb) echo $crumb . ' ' . $delimiter . ' ';

echo $currentBefore;

the_title();

echo $currentAfter;

?

} elseif ( is_search() ) {

echo $currentBefore . 'Search results for &#39;' . get_search_query() . '&#39;' . $currentAfter;

?

} elseif ( is_tag() ) {

echo $currentBefore . 'Posts tagged &#39;';

single_tag_title();

echo '&#39;' . $currentAfter;

?

} elseif ( is_author() ) {

global $author;

$userdata = get_userdata($author);

echo $currentBefore . 'Articles posted by ' . $userdata->display_name . $currentAfter;

?

} elseif ( is_404() ) {

echo $currentBefore . 'Error 404' . $currentAfter;

}

?

if ( get_query_var('paged') ) {

if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';

echo __('Page') . ' ' . get_query_var('paged');

if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';

}

?

echo '</div>';

?

}

}

最后在适当的地方(如方法一中提到的几个文件)添加以下代码调用:

<div class="mbx-dh">

<?php if (function_exists('dimox_breadcrumbs')) dimox_breadcrumbs(); ?>

</div>

如果想要美化下显示方式,直接通过添加 css 即可:

.mbx-dh {padding: 5px 10px;}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读