今天说一下在wp/wordpress中面包屑导航的调用方式,这里有2个自定义的方法,也都是从网上收集来的,大家试着用,不多哔哔,直接上代码!

wp主题开发

方法一:

在functions.php中添加:

//面包屑功能
function qui_breadcrumb() {
    echo '<ul class="breadcrumb">';
if (!is_home()) {
    echo '<li><a href="';
    echo get_option('home');
    echo '">';
    echo '首页';
    echo "</a></li>";
    if (is_category() || is_single()) {
            echo '<li>';
            the_category(' </li><li> ');
            if (is_single()) {
                    echo "</li><li>";
                    the_title();
                    echo '</li>';
            }
    } elseif (is_page()) {
            echo '<li>';
            echo the_title();
            echo '</li>';
    }
}
elseif (is_tag()) {single_tag_title();}
elseif (is_day()) {echo"<li>Archive for "; the_time('F jS, Y'); echo'</li>';}
elseif (is_month()) {echo"<li>Archive for "; the_time('F, Y'); echo'</li>';}
elseif (is_year()) {echo"<li>Archive for "; the_time('Y'); echo'</li>';}
elseif (is_author()) {echo"<li>Author Archive"; echo'</li>';}
elseif (isset($_GET['paged']) && !empty($_GET['paged'])) {echo "<li>Blog Archives"; echo'</li>';}
elseif (is_search()) {echo"<li>Search Results"; echo'</li>';}
echo '</ul>';
}

2.在文章中调用

<div class="breadwrap"><?php if(function_exists('qui_breadcrumb')) qui_breadcrumb();?></div>

不过这个方法有局限,只能在二级页面,三级页面用,首页不要用啊!调不出来数据!

方法二:来自秀主题

1.functions.php中添加

function hui_breadcrumbs(){
    if( !is_single() ) return false;
    $categorys = get_the_category();
    $category = $categorys[0];
    
    return '<span class="text-muted">当前位置:</span><a href="'.get_bloginfo('url').'">'.get_bloginfo('name').'</a> <small>></small> '.get_category_parents($category->term_id, true, ' <small>></small> ').'<span class="text-muted">'.(!_hui('breadcrumbs_single_text')?get_the_title():'正文').'</span>';
}

2.在需要的地方直接调用

<?php hui_breadcrumbs(); ?>