教你制作wordpress面包屑导航

时间:2019-09-14
阅读:

面包屑对于一个网站来说,相当于是页面结构的一个导航,是网页导航设计中一个标准设计模式,而今天我们讲的是如何通过代码来实现wordpress面包屑导航的功能,本站的老访客都知道,小编在用wordpress主题的时候十分的不喜欢用插件,尽管很方便,但会降低网站的打开速度!

教你制作wordpress面包屑导航

教程开始了:

function get_breadcrumbs()  
{  
    global $wp_query;  
    
    if ( !is_home() ){  
    
        // Start the UL  
        echo '<div class="entry clearfix breadcrumbs">';  
        // Add the Home link  
        echo '<a href="'. get_settings('home') .'">'. get_bloginfo('name') .'</a>';  
    
        if ( is_category() )  
        {  
            $catTitle = single_cat_title( "", false );  
            $cat = get_cat_ID( $catTitle );  
            echo " &raquo; ". get_category_parents( $cat, TRUE, " &raquo; " ) ."";  
        }  
        elseif ( is_archive() && !is_category() )  
        {  
            echo " &raquo; Archives";  
        }  
        elseif ( is_search() ) {  
    
            echo " &raquo; Search Results";  
        }  
        elseif ( is_404() )  
        {  
            echo " &raquo; 404 Not Found";  
        }  
        elseif ( is_single() )  
        {  
            $category = get_the_category();  
            $category_id = get_cat_ID( $category[0]->cat_name );  
    
            echo ' &raquo; '. get_category_parents( $category_id, TRUE, " &raquo; " );  
            echo the_title('','', FALSE) ."";  
        }  
        elseif ( is_page() )  
        {  
            $post = $wp_query->get_queried_object();  
    
            if ( $post->post_parent == 0 ){  
    
                echo " &raquo; ".the_title('','', FALSE)."";  
    
            } 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 ' &raquo; <a href="'. get_permalink($ancestor) .'">'. strip_tags( apply_filters( 'single_post_title', get_the_title( $ancestor ) ) ) .'</a>';  
                    } else {  
                        echo ' &raquo; '. strip_tags( apply_filters( 'single_post_title', get_the_title( $ancestor ) ) ) .'';  
                    }  
                }  
            }  
        }  
    
        // End the UL  
        echo "</div>";  
    }  
}

将上面的代码复制进wordpress主题文件夹下的functions.php中,然后,我们开始调用它

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

将上面的调用函数放进wordpress主题文件下的archive.php、single.php、index.php、search.php等页面的相应位置,当然这是你想放哪就放哪,只要你觉得美观就好,我们都习惯放文章上方,header的下方。。。

再将这段CSS放进主题文件下的css里即可。。。

.entry {
    margin-bottom: 20px;
    padding: 20px;
    background: #FFF;
    box-shadow: 0 0 5px rgba(0,0,0,0.2);
    -moz-box-shadow: 0 0 5px rgba(0,0,0,0.2);
    -webkit-box-shadow: 0 0 20px rgba(0,0,0,0.2);
    position: relative;
}
.breadcrumbs {
    padding: 10px 20px;
    font-size: 14px;
}

这样wordpress面包屑导航的功能基本就大功告成了,刷新下页面试试哈,样式效果都还不错吧!

返回顶部
顶部