美文网首页
给博客增加面包屑

给博客增加面包屑

作者: 一叶一华年 | 来源:发表于2019-05-14 17:26 被阅读0次

    外观--编辑--主题文件--function.php

    
    function get_breadcrumbs() {
    
        global $wp_query;
    
        if (!is_home()) {
    
            // Start the UL 
    
            echo '<ul class="breadcrumbs">';
    
            // Add the Home link 
    
            echo '<li>您的位置:  <a href="http://www.udesk.cn/">首页 > </a></li>';
    
            if (is_category()) {
    
                $catTitle = single_cat_title("", false);
    
                $cat = get_cat_ID($catTitle);
    
                echo "<li> ".get_category_parents($cat, TRUE).
    
                "</li>";
    
            }
    
            elseif(is_archive() && !is_category()) {
    
                echo "<li> &gt; Archives</li>";
    
            }
    
            elseif(is_search()) {
    
                echo "<li> &gt; Search Results</li>";
    
            }
    
            elseif(is_404()) {
    
                echo "<li> &gt; 404 Not Found</li>";
    
            }
    
            elseif(is_single()) {
    
                $category = get_the_category();
    
                $category_id = get_cat_ID($category[0] - > cat_name);
    
                echo '<li> '.get_category_parents($category_id, TRUE, " &gt; ");
    
                echo the_title('', '', FALSE).
    
                "</li>";
    
            }
    
            elseif(is_page()) {
    
                $post = $wp_query - > get_queried_object();
    
                if ($post - > post_parent == 0) {
    
                    echo "<li> ".the_title('', '', FALSE).
    
                    "</li>";
    
                } else {
    
                    $title = the_title('', '', FALSE);
    
                    $ancestors = array_reverse(get_post_ancestors($post - > ID));
    
                    array_push($ancestors, $post - > ID);
    
                    foreach($ancestors as $ancestor) {
    
                        if ($ancestor != end($ancestors)) {
    
                            echo '<li><a href="'.get_permalink($ancestor).
    
                            '">'.strip_tags(apply_filters('single_post_title', get_the_title($ancestor))).
    
                            '</a></li>';
    
                        } else {
    
                            echo '<li> '.strip_tags(apply_filters('single_post_title', get_the_title($ancestor))).
    
                            '</li>';
    
                        }
    
                    }
    
                }
    
            }
    
            // End the UL 
    
            echo "</ul>";
    
        }
    
    }
    
    

    然后在header.php中添加一行代码

    <?php if (function_exists('get_breadcrumbs')){get_breadcrumbs(); } ?>  
    
    image.png

    相关文章

      网友评论

          本文标题:给博客增加面包屑

          本文链接:https://www.haomeiwen.com/subject/cojeaqtx.html