WordPress面包屑导航

WordPress面包屑导航

// Add Breadcrumb Navigation, original article on: https://bohanintl.com/wptips/?p=37
function so_write_breadcrumb() {
    $pid = $post->ID;
    $trail = '<a href="/">'. __('Home', 'so-base') .'</a>';
    if (is_front_page()) {
        // do nothing
    } elseif (is_page()) {
        $bcarray = array();
        $pdata = get_post($pid);
        $bcarray[] = ' » '.$pdata->post_title."\n";
        while ($pdata->post_parent) {
            $pdata = get_post($pdata->post_parent);
            $bcarray[] = ' » <a href="'.get_permalink($pdata->ID).'">'.$pdata->post_title.'</a>';
        }
        $bcarray = array_reverse($bcarray);
        foreach ($bcarray AS $listitem) {
            $trail .= $listitem;
        }
    } elseif (is_single()) {
        $pdata = get_the_category($pid);
        $adata = get_post($pid);
        if(!empty($pdata)) {
            $data = get_category_parents($pdata[0]->cat_ID, TRUE, ' » ');
            $trail .= " » ".substr($data,0,-8);
        }
        $trail.= ' » '.$adata->post_title."\n";
    } elseif (is_category()) {
        $pdata = get_the_category($pid);
        $data = get_category_parents($pdata[0]->cat_ID, TRUE, ' » ');
        if(!empty($pdata)) {
            $trail .= " » ".substr($data,0,-8);
        }
    }
    $trail.="";
    return $trail;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据