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

WordPress 添加面包屑导航的二种方法,第1张

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

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。内存溢出小编现在分享给大家,也给大家做个参考。

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

面包屑定义:

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


面包屑好处:

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

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

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

方案一:

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

<div >

当前位置:<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 >

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

</div>

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

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

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的WordPress 添加面包屑导航的二种方法全部内容,希望文章能够帮你解决WordPress 添加面包屑导航的二种方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/1013467.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存