非插件调用wordpress面包屑导航
为了方便用户浏览,和搜索引擎读取网页,我们常常需要在网站中添加面包屑导航
例如:“当前的位置:首页->新闻中心->体育频道”。
通过这句导航可以看到自己当前阅读的文章位置。这个就是面包屑导航了。
它们绝大部分看起来就像这样:首页→分类页→次级分类页。
我们只需要两个步骤就可以轻松添加网站面包屑导航。
第一在wordpress主题函数functions.php中添加如下代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
function wheatv_breadcrumbs() { $delimiter = ' > '; $name = '首页'; // if ( !is_home() ||!is_front_page() || is_paged() ) { global $post; $home = get_bloginfo('url'); echo '<a href="' . $home . '" class="gray">' . $name . '</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) echo(get_category_parents($parentCat, TRUE, ' ' . $delimiter . ' ')); echo single_cat_title(); } elseif ( is_day() ) { echo '<a href="' . get_year_link(get_the_time('Y')) . '" class="gray">' . get_the_time('Y') . '</a> ' . $delimiter . ' '; echo '<a href="' . get_month_link(get_the_time('Y'),get_the_time('m')) . '" class="gray">' . get_the_time('F') . '</a> ' . $delimiter . ' '; echo get_the_time('d'); } elseif ( is_month() ) { echo '<a href="' . get_year_link(get_the_time('Y')) . '" class="gray">' . get_the_time('Y') . '</a> ' . $delimiter . ' '; echo get_the_time('F'); } elseif ( is_year() ) { echo get_the_time('Y'); } elseif ( is_single() ) { $cat = get_the_category(); $cat = $cat[0]; echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' '); echo "正文"; } elseif ( is_page()||!$post->post_parent ) { the_title(); } elseif ( is_page()||$post->post_parent ) { $parent_id = $post->post_parent; $breadcrumbs = array(); while ($parent_id) { $page = get_page($parent_id); $breadcrumbs[] = '<a href="https://www.wheatv.com/site/wp-admin/ . get_permalink($page->ID) . " class="gray">' . get_the_title($page->ID) . '</a>'; $parent_id = $page->post_parent; } $breadcrumbs = array_reverse($breadcrumbs); foreach ($breadcrumbs as $crumb) echo $crumb . ' ' . $delimiter . ' '; the_title(); } elseif ( is_search() ) { echo get_search_query(); } elseif ( is_tag() ) { echo single_tag_title(); } elseif ( is_author() ) { global $author; $userdata = get_userdata($author); echo '由'.$userdata->display_name.'发表'; } elseif ( is_404() ) { echo '404 错误'; } if ( get_query_var('paged') ) { if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' ('; echo '第' . ' ' . get_query_var('paged').' 页'; if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')'; } }else{ echo $name; } } |
第二步,在需要调用面包屑导航的位置写入调用标签;
1 |
<?php wheatv_breadcrumbs(); ?> |
就可以在对应的位置调用面包屑导航了!
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END