下载鸥 > 网站下载 > 开发教程 > WordPress

分享一段WordPress面包屑导航源码

344 2022-03-27 13:25:48

收藏
WordPress博客程序非常好用,但他的一个缺点是不带面包屑导航。而面包屑导航在seo中又是非常重要的。那么,我们要怎么自己开发呢?

分享一段WordPress面包屑导航源码
下方源码供参考。
 

WordPress面包屑导航源码


function wp_breadcrumb()
{
    $delimiter = ' » ';
    $home = '首页';
    $search_result = get_option('op_search_result');
    $posts_tagged = get_option('op_posts_tagged');
    $posted_author = get_option('op_posted_author');
    $error_404 = get_option('op_error_404');
    $page_navi = get_option('op_page_navi');
    $before = '';
    $after = '';
    if (!is_home() && !is_front_page() || is_paged()) {
        global $post;
        $homeLink = home_url();
        echo '<a href="' . $homeLink . '">' . $home . '</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 $before . single_cat_title('', false) . $after;
        } elseif (is_day()) {
            echo '' . get_the_time('Y') . '年' . get_the_time('m') . '月' . get_the_time('d') . '日发布的内容' . $after . '';
        } elseif (is_month()) {
            echo '' . get_the_time('Y') . '年' . get_the_time('m') . '月发布的内容' . $after . '';
        } elseif (is_year()) {
            echo '' . 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 href="' . $homeLink . '/' . $slug['slug'] . '/">' . $post_type->labels->singular_name . '</a>' . $delimiter . '';
                echo $before . get_the_title() . $after;
            } else {
                $cat = get_the_category();
                $cat = $cat[0];
                echo get_category_parents($cat, TRUE, '' . $delimiter . '');
                echo $before . get_the_title() . $after;
            }
        } elseif (!is_single() && !is_page() && get_post_type() != 'post' && !is_404()) {
            $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 get_category_parents($cat, TRUE, '' . $delimiter . '');
            echo '<a 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 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 . '' . $search_result . '' . get_search_query() . '(搜索结果)' . $after;
        } elseif (is_tag()) {
            echo $before . '' . $posts_tagged . '' . single_tag_title('', false) . '(标签)' . $after;
        } elseif (is_author()) {
            global $author;
            $userdata = get_userdata($author);
            echo $before . '' . $posted_author . '' . $userdata->display_name . '发布的内容' . $after;
        } elseif (is_404()) {
            echo $before . '' . $error_404 . '404错误' . $after;
        }
        if (get_query_var('page')) {
            echo '(第';
            echo get_query_var('page');
            echo '页)';
        }
        if (get_query_var('paged')) {
            if (is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author()) {
                echo '(第';
            }
            echo __('' . $page_navi . '') . '' . get_query_var('paged');
            if (is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author()) {
                echo '页)';
            }
        }
    }
} 
 

WordPress面包屑导航使用方法

将以上代码复制并粘贴到主题文件的functions.php文件中,然后在对应模板中调用即可实现面包屑导航的制作。

这样,我们就实现了WordPress面包屑导航的制作。

本文地址:https://xzo.com.cn/develop/wp/1075.html

有帮助,很赞!

信息来源:下载鸥
信息标签
导出教程 下载word版教程
发表评论 共有条评论
  • 隐藏的大佬 2022-03-27 15:43:20评论 #沙发#

    测试了好多段都不行,总算可以了,感谢大佬无私分享

关于WordPress


WordPress是世界级的博客程序,有过百万的模板插件支持,其外型大多十分美观,功能十分强大,可作为博客用户的首选。但如果是企业网站建设,则不建议选用WordPress:数据量是个大问题。

推荐WordPress开发教程
不懂开发,怎样用WordPress建站?
不懂开发,怎样用WordPress建站?

目前市面上的第三方建站程序很多,但使用WordPress建站无疑具备了高效、美观...

17 1202
WordPress怎样搬家/更换域名?搬家后乱码/跳转/404怎么办?
WordPress怎样搬家/更换域名?搬家后乱码/跳转/404怎么办?

对于建站时间稍长的站长来说,换域名、换服务器、搬家都是必要的操作。如果使...

10 1670
Wordpress怎样设置短代码?
Wordpress怎样设置短代码?

我知道wordpress有神奇的自定义函数,这个自定义函数基本上可以代替大部分的...

17 300
WordPress信息时间改为多少时间前发表
WordPress信息时间改为多少时间前发表

相对日期,文章或者评论发表日期以“发表于1小时前”,这种形式显示,相对日期会...

9 338
wordpress怎样调用注册会员发表的文章数量?
wordpress怎样调用注册会员发表的文章数量?

由于客户需要在wordpress展示会员发布的信息总量,但wordpress默认是没有这个...

0 240
wordpress的模板配置方法
wordpress的模板配置方法

wordpress是全世界最流行的博客程序,国内用wordpress建站的站长占比也很高,当...

0 363
wordpress新手:更换域名后访问所有页面都跳转之前的域名
wordpress新手:更换域名后访问所有页面都跳转之前的域名

wordpress数据库里写了很多与域名相关的东西,如果更换了域名,需要对域名进行...

0 359
分享一段WordPress面包屑导航源码
分享一段WordPress面包屑导航源码

WordPress博客程序非常好用,但他的一个缺点是不带面包屑导航。而面包屑导航...

1 343
随机WordPress开发教程
怎样去掉WordPress目录链接中category?
怎样去掉WordPress目录链接中category?

WordPress自带的伪静态虽然简单,但并不好用。自带的category目录虽然让层级...

0 373
WordPress怎样删除垃圾代码global-styles-inline-css和duotone svg
WordPress怎样删除垃圾代码global-styles-inline-css和duotone svg

WordPress默认头部内联样式 global-styles-inline-css 和底部 duotone svg...

0 432
wordpress怎样修改后台登录地址
wordpress怎样修改后台登录地址

WordPress是全世界范围内使用人数最多的博客程序,但正因为用户太多,WordPress...

0 304
WordPress怎样获取文章第一张缩略图
WordPress怎样获取文章第一张缩略图

WordPress有缩略图字段,但如果没有录入缩略图,能否自动获取文章内的第一张图...

0 323
wp_nav_menu调用菜单不展示是什么原因?
wp_nav_menu调用菜单不展示是什么原因?

一位初学WordPress开发的朋友问鸥哥问题:wp_nav_menu调用菜单无法展示。其调...

0 257
wordpress新手知识:怎样修改主题名称?
wordpress新手知识:怎样修改主题名称?

WordPress建站主题经常是五花八门的,有些人就想要改动,但不知道该如何改。其...

0 269
WordPress站点配置SSL证书启用https的方法
WordPress站点配置SSL证书启用https的方法

https比http更安全,正规网站基本都启用了https。那么,WordPress搭建的站点怎...

0 727
wordpress新手:更换域名后访问所有页面都跳转之前的域名
wordpress新手:更换域名后访问所有页面都跳转之前的域名

wordpress数据库里写了很多与域名相关的东西,如果更换了域名,需要对域名进行...

0 359
客服QQ:341553759
点击咨询 常见问题 >
官方交流群:90432500
点击加入