wordpressでぱんくずリストをプラグイン利用せずで作成する。

function pankuzu(){
    global $post;
    $str ='';
    if(!is_home()&&!is_admin()){
        $str.= '<div class="pankuzu"><ul>';
        $str.= '<li><a href="'. home_url() .'"><span class="parent_title">ホーム</span></a> ></li>';
 
        if(is_category()) {
            $cat = get_queried_object();
            if($cat -> parent != 0){
                $ancestors = array_reverse(get_ancestors( $cat -> cat_ID, 'category' ));
                foreach($ancestors as $ancestor){
                    $str.='<li><a href="'. get_category_link($ancestor) .'"><span class="parent_title">'. get_cat_name($ancestor) .'</span></a> ></li>';
                }
            }
	        $str.='<li><a href="'. get_category_link($cat -> term_id). '"><span class="page_title">'. $cat-> cat_name . '</span></a></li>';
        } elseif(is_page()) {
            if($post -> post_parent != 0 ){
                $ancestors = array_reverse(get_post_ancestors( $post->ID ));
                foreach($ancestors as $ancestor){
                    $str.='<li><a href="'. get_permalink($ancestor).'"><span class="parent_title">'. get_the_title($ancestor) .'</span></a></li>';
                }
            } else {
            	$str.='<li><span class="page_title">'. get_the_title($ancestor) .'</span></li>';
            }
        } elseif(is_single()) {
            $categories = get_the_category($post->ID);
            $cat = $categories[0];
            if($cat -> parent != 0){
                $ancestors = array_reverse(get_ancestors( $cat -> cat_ID, 'category' ));
                foreach($ancestors as $ancestor){
                    $str.='<li><a href="'. get_category_link($ancestor).'"><span class="parent_title">'. get_cat_name($ancestor). '</span></a> ></li>';
                }
                $str.='<li><a href="'. get_category_link($cat -> term_id). '"><span class="parent_title">'. $cat-> cat_name . '</span></a></li>';
            }
            $archives = get_post_type_object(get_post_type());
            $archive_flag = $archives->has_archive;
            if($archive_flag) {
            	$str.='<li><a href="'. home_url() ."/". get_post_type() .'"><span class="parent_title">'. $archives->labels->name . '</span></a> ></li>';
            }
            $str.='<li><span class="page_title">'. wp_title('', false) .'</span></li>';
        } else {
            $str.='<li><span class="page_title">'. wp_title('', false) .'</span></li>';
        }
        $str.='</ul></div>';
    }
    echo $str;
}