魔扣论坛

魔扣源码论坛业务
查看: 1281|回复: 5

[经验交流] WordPress 面包屑导航Breadcrumbs 非插件实现

[复制链接]
  • TA的每日心情
    慵懒
    8 小时前
  • 签到天数: 2957 天

    [LV.Master]开坛老将

    7万

    主题

    227

    回帖

    27万

    积分

    管理员

    Rank: 30Rank: 30Rank: 30Rank: 30Rank: 30Rank: 30Rank: 30Rank: 30

    魔扣币
    745759
    贡献
    157915
    威望
    32799

    最佳新人活跃会员热心会员推广达人宣传达人突出贡献优秀版主荣誉管理论坛元老

    发表于 2017-6-15 17:35:29 | 显示全部楼层 |阅读模式
    魔扣币兑换比例:【 50以下 : ¥1 = 10 魔扣币 】丨【 50 - 100 :¥1 = 20 魔扣币】丨【 100以上:¥1 = 30 魔扣币 】

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    x
    面包屑导航对于一个网站SEO来说还是挺重要的,可以使用以下代码来给自己的主题添加面包屑,将以下代码加入到主题的functions.php里:
    1. function MBThemes_breadcrumbs() {
    2. /* === OPTIONS === */
    3. $text['home'] = '首页';
    4. $text['category'] = '%s';
    5. $text['search'] = '搜索结果:%s';
    6. $text['tag'] = '标签:%s';
    7. $text['author'] = '作者:%s';
    8. $text['404'] = '404';
    9. $text['page'] = '%s';
    10. $text['cpage'] = '%s';
    11. $wrap_before = '<div class="breadcrumbs">';
    12. $wrap_after = '</div>';
    13. $sep = '>';
    14. $sep_before = '<span class="sep">';
    15. $sep_after = '</span>';
    16. $show_home_link = 1;
    17. $show_on_home = 0;
    18. $show_current = 1;
    19. $before = '<span class="current">';
    20. $after = '</span>';
    21. /* === END OF OPTIONS === */
    22. global $post;
    23. $home_link = home_url('/');
    24. $link_before = '<span itemscope itemtype="http://data-vocabulary.org/Breadcrumb">';
    25. $link_after = '</span>';
    26. $link_attr = ' itemprop="url"';
    27. $link_in_before = '<span itemprop="title">';
    28. $link_in_after = '</span>';
    29. $link = $link_before . '<a href="%1$s"' . $link_attr . '>' . $link_in_before . '%2$s' . $link_in_after . '</a>' . $link_after;
    30. $frontpage_id = get_option('page_on_front');
    31. $parent_id = $post->post_parent;
    32. $sep = ' ' . $sep_before . $sep . $sep_after . ' ';
    33. if (is_home() || is_front_page()) {
    34. if ($show_on_home) echo $wrap_before . '<a href="' . $home_link . '">' . $text['home'] . '</a>' . $wrap_after;
    35. } else {
    36. echo $wrap_before;
    37. if ($show_home_link) echo sprintf($link, $home_link, $text['home']);
    38. if ( is_category() ) {
    39. $cat = get_category(get_query_var('cat'), false);
    40. if ($cat->parent != 0) {
    41. $cats = get_category_parents($cat->parent, TRUE, $sep);
    42. $cats = preg_replace("#^(.+)$sep$#", "$1", $cats);
    43. $cats = preg_replace('#<a([^>]+)>([^<]+)<\/a>#', $link_before . '<a$1' . $link_attr .'>' . $link_in_before . '$2' . $link_in_after .'</a>' . $link_after, $cats);
    44. if ($show_home_link) echo $sep;
    45. echo $cats;
    46. }
    47. if ( get_query_var('paged') ) {
    48. $cat = $cat->cat_ID;
    49. echo $sep . sprintf($link, get_category_link($cat), get_cat_name($cat)) . $sep . $before . sprintf($text['page'], get_query_var('paged')) . $after;
    50. } else {
    51. if ($show_current) echo $sep . $before . sprintf($text['category'], single_cat_title('', false)) . $after;
    52. }
    53. } elseif ( is_search() ) {
    54. if (have_posts()) {
    55. if ($show_home_link && $show_current) echo $sep;
    56. if ($show_current) echo $before . sprintf($text['search'], get_search_query()) . $after;
    57. } else {
    58. if ($show_home_link) echo $sep;
    59. echo $before . sprintf($text['search'], get_search_query()) . $after;
    60. }
    61. } elseif ( is_day() ) {
    62. if ($show_home_link) echo $sep;
    63. echo sprintf($link, get_year_link(get_the_time('Y')), get_the_time('Y')) . $sep;
    64. echo sprintf($link, get_month_link(get_the_time('Y'), get_the_time('m')), get_the_time('F'));
    65. if ($show_current) echo $sep . $before . get_the_time('d') . $after;
    66. } elseif ( is_month() ) {
    67. if ($show_home_link) echo $sep;
    68. echo sprintf($link, get_year_link(get_the_time('Y')), get_the_time('Y'));
    69. if ($show_current) echo $sep . $before . get_the_time('F') . $after;
    70. } elseif ( is_year() ) {
    71. if ($show_home_link && $show_current) echo $sep;
    72. if ($show_current) echo $before . get_the_time('Y') . $after;
    73. } elseif ( is_single() && !is_attachment() ) {
    74. if ($show_home_link) echo $sep;
    75. if ( get_post_type() != 'post' ) {
    76. $post_type = get_post_type_object(get_post_type());
    77. $slug = $post_type->rewrite;
    78. printf($link, $home_link . '/' . $slug['slug'] . '/', $post_type->labels->singular_name);
    79. if ($show_current) echo $sep . $before . get_the_title() . $after;
    80. } else {
    81. $cat = get_the_category(); $cat = $cat[0];
    82. $cats = get_category_parents($cat, TRUE, $sep);
    83. if (!$show_current || get_query_var('cpage')) $cats = preg_replace("#^(.+)$sep$#", "$1", $cats);
    84. $cats = preg_replace('#<a([^>]+)>([^<]+)<\/a>#', $link_before . '<a$1' . $link_attr .'>' . $link_in_before . '$2' . $link_in_after .'</a>' . $link_after, $cats);
    85. echo $cats;
    86. if ( get_query_var('cpage') ) {
    87. echo $sep . sprintf($link, get_permalink(), get_the_title()) . $sep . $before . sprintf($text['cpage'], get_query_var('cpage')) . $after;
    88. } else {
    89. if ($show_current) echo $before . get_the_title() . $after;
    90. }
    91. }
    92. // custom post type
    93. } elseif ( !is_single() && !is_page() && get_post_type() != 'post' && !is_404() ) {
    94. $post_type = get_post_type_object(get_post_type());
    95. if ( get_query_var('paged') ) {
    96. echo $sep . sprintf($link, get_post_type_archive_link($post_type->name), $post_type->label) . $sep . $before . sprintf($text['page'], get_query_var('paged')) . $after;
    97. } else {
    98. if ($show_current) echo $sep . $before . $post_type->label . $after;
    99. }
    100. } elseif ( is_attachment() ) {
    101. if ($show_home_link) echo $sep;
    102. $parent = get_post($parent_id);
    103. $cat = get_the_category($parent->ID); $cat = $cat[0];
    104. if ($cat) {
    105. $cats = get_category_parents($cat, TRUE, $sep);
    106. $cats = preg_replace('#<a([^>]+)>([^<]+)<\/a>#', $link_before . '<a$1' . $link_attr .'>' . $link_in_before . '$2' . $link_in_after .'</a>' . $link_after, $cats);
    107. echo $cats;
    108. }
    109. printf($link, get_permalink($parent), $parent->post_title);
    110. if ($show_current) echo $sep . $before . get_the_title() . $after;
    111. } elseif ( is_page() && !$parent_id ) {
    112. if ($show_current) echo $sep . $before . get_the_title() . $after;
    113. } elseif ( is_page() && $parent_id ) {
    114. if ($show_home_link) echo $sep;
    115. if ($parent_id != $frontpage_id) {
    116. $breadcrumbs = array();
    117. while ($parent_id) {
    118. $page = get_page($parent_id);
    119. if ($parent_id != $frontpage_id) {
    120. $breadcrumbs[] = sprintf($link, get_permalink($page->ID), get_the_title($page->ID));
    121. }
    122. $parent_id = $page->post_parent;
    123. }
    124. $breadcrumbs = array_reverse($breadcrumbs);
    125. for ($i = 0; $i < count($breadcrumbs); $i++) {
    126. echo $breadcrumbs[$i];
    127. if ($i != count($breadcrumbs)-1) echo $sep;
    128. }
    129. }
    130. if ($show_current) echo $sep . $before . get_the_title() . $after;
    131. } elseif ( is_tag() ) {
    132. if ( get_query_var('paged') ) {
    133. $tag_id = get_queried_object_id();
    134. $tag = get_tag($tag_id);
    135. echo $sep . sprintf($link, get_tag_link($tag_id), $tag->name) . $sep . $before . sprintf($text['page'], get_query_var('paged')) . $after;
    136. } else {
    137. if ($show_current) echo $sep . $before . sprintf($text['tag'], single_tag_title('', false)) . $after;
    138. }
    139. } elseif ( is_author() ) {
    140. global $author;
    141. $author = get_userdata($author);
    142. if ( get_query_var('paged') ) {
    143. if ($show_home_link) echo $sep;
    144. echo sprintf($link, get_author_posts_url($author->ID), $author->display_name) . $sep . $before . sprintf($text['page'], get_query_var('paged')) . $after;
    145. } else {
    146. if ($show_home_link && $show_current) echo $sep;
    147. if ($show_current) echo $before . sprintf($text['author'], $author->display_name) . $after;
    148. }
    149. } elseif ( is_404() ) {
    150. if ($show_home_link && $show_current) echo $sep;
    151. if ($show_current) echo $before . $text['404'] . $after;
    152. } elseif ( has_post_format() && !is_singular() ) {
    153. if ($show_home_link) echo $sep;
    154. echo get_post_format_string( get_post_format() );
    155. }
    156. echo $wrap_after;
    157. }
    158. }
    复制代码
    然后在需要显示面包屑的地方加上以下代码:
    1. <?php if (function_exists('MBThemes_breadcrumbs')) MBThemes_breadcrumbs(); ?>
    复制代码


    会员购买:>> 点击购买 << | 魔扣币购买:>> 点击购买 <<
    承接业务:服务器代维丨网站托管丨SEO
    联系客服:微信:morko-net | QQ:1367681973

    该用户从未签到

    1

    主题

    260

    回帖

    521

    积分

    高级魔扣

    Rank: 4

    魔扣币
    260
    贡献
    260
    威望
    0
    发表于 2017-6-15 18:10:14 | 显示全部楼层
    轻轻的打开你的空间,让你知道我的到来!慢慢放飞我的祝福,让你感到我的存在!让风儿送去我的心声,让星星传递我的问候!魔扣源码论坛愿我的到访让你永远开心快乐!

    该用户从未签到

    1

    主题

    260

    回帖

    522

    积分

    高级魔扣

    Rank: 4

    魔扣币
    261
    贡献
    261
    威望
    0
    发表于 2017-10-29 14:08:54 | 显示全部楼层
    当你孤独时,风儿就是我的歌声,愿它能使你得到片刻的安慰;当你骄傲时,雨点就是我的警钟,魔扣源码论坛愿它能使你获得永恒的谦逊。

    该用户从未签到

    0

    主题

    274

    回帖

    548

    积分

    高级魔扣

    Rank: 4

    魔扣币
    274
    贡献
    274
    威望
    0
    发表于 2018-2-6 22:43:47 | 显示全部楼层
    欢迎你,亲爱的朋友,欢迎您光临并留下美好的祝福。相信通过网络的交流,我们会从陌生到相识相知,不断增进彼此信任,友谊会不断加深,这是我们前世修来的缘分。让我们珍惜这份缘,魔扣源码论坛愿我们友谊永存!

    该用户从未签到

    0

    主题

    265

    回帖

    530

    积分

    高级魔扣

    Rank: 4

    魔扣币
    265
    贡献
    265
    威望
    0
    发表于 2018-8-19 08:54:44 | 显示全部楼层
    太漂亮的源码了,非常感谢魔扣源码论坛

    该用户从未签到

    0

    主题

    253

    回帖

    506

    积分

    高级魔扣

    Rank: 4

    魔扣币
    253
    贡献
    253
    威望
    0
    发表于 2019-2-2 18:22:30 来自手机 | 显示全部楼层
    是爷们的娘们的都帮顶!大力支持
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    • 联系我们
    • 新浪微博 :
    • 在线客服 :魔扣科技 
    • 源码QQ群 :魔扣源码论坛官方总群
    • 联系邮箱 :charlin#morko.net
    • 微信扫一扫
    快速回复 返回顶部 返回列表