うさぎのイラスト

ネットアンサー55備忘録

web技術を書いていきます

パンくずリストの作成

-2015年07月07日-
functions.phpに下記を書き足す。
1// パンくずリスト
2function breadcrumb(){
3    global $post;
4    $str ='';
5    if(!is_home()&&!is_admin()){
6        $str.= '<div id="breadcrumb" class="cf"><div itemscope itemtype="http://data-vocabulary.org/Breadcrumb">';
7        $str.= '<a href="'. home_url() .'" itemprop="url"><span itemprop="title">ホーム</span></a> &gt;</div>';
8 
9        if(is_category()) {
10            $cat = get_queried_object();
11            if($cat -> parent != 0){
12                $ancestors = array_reverse(get_ancestors( $cat -> cat_ID, 'category' ));
13                foreach($ancestors as $ancestor){
14                    $str.='<div itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_category_link($ancestor) .'" itemprop="url"><span itemprop="title">'. get_cat_name($ancestor) .'</span></a> &gt;</div>';
15                }
16            }
17        $str.='<div itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_category_link($cat -> term_id). '" itemprop="url"><span itemprop="title">'. $cat-> cat_name . '</span></a></div>';
18        } elseif(is_page()){
19            if($post -> post_parent != 0 ){
20                $ancestors = array_reverse(get_post_ancestors( $post->ID ));
21                foreach($ancestors as $ancestor){
22                    $str.='<div itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_permalink($ancestor).'" itemprop="url"><span itemprop="title">'. get_the_title($ancestor) .'</span></a></div>';
23                }
24            }
25        } elseif(is_single()){
26            $categories = get_the_category($post->ID);
27            $cat = $categories[0];
28            if($cat -> parent != 0){
29                $ancestors = array_reverse(get_ancestors( $cat -> cat_ID, 'category' ));
30                foreach($ancestors as $ancestor){
31                    $str.='<div itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_category_link($ancestor).'" itemprop="url"><span itemprop="title">'. get_cat_name($ancestor). '</span></a> &gt;</div>';
32                }
33            }
34            $str.='<div itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_category_link($cat -> term_id). '" itemprop="url"><span itemprop="title">'. $cat-> cat_name . '</span></a></div>';
35        } else{
36            $str.='<div>'. wp_title('', false) .'</div>';
37        }
38        $str.='</div>';
39    }
40    echo $str;
41}
パンくずリストを表示したいところに書きを書き足す。 一列に表示をしたかったらcssに書き込む
1#breadcrumb div{
2    display:inline;
3}