HOME
Tech
Bread columns for AMP without plugin
カテゴリー
Tech

Bread columns for AMP without plugin

日本語のページはこちらです

Make bread columns for AMP without plugin.

Bread columns is the following. It shows your location in web site. I just knew it now!

A>B>C

Why is it named bread columns? It is from old tales. Bread columns is the mark to get home.

Make bread columns

Anyway, I take in it my page. But accident. Plugin is not working at my page. Is it cause AMP page? So, I try to make it myself. I refer to the following site.

Coding of ウィンドミル.inc

Several points is modified.

  • Change reference from data-vocabulary.org to schema.org.
    • Google recommend
  • Change of describe stuctured data.
  • Show current location.

Code

Add the following code in your child-thema.

<?php
function breadcrumb(){
global $post;
    $str ='';
    if(!is_home()&&!is_admin()){
		$str.= '<div itemscope itemtype="http://schema.org/BreadcrumbList">';
		$str.= '<div itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem" >';
		$str.= '<meta itemprop="position" content="1">';
     	$str.= '<a itemprop="item" href="'. home_url() .'"><span itemprop="name">HOME</span></a> &gt;&#160;</div>';	
        if(is_page()){
            if($post -> post_parent != 0 ){
                $ancestors = array_reverse(get_post_ancestors( $post->ID ));
                foreach($ancestors as $ancestor){
				$str.= '<div itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem" >';
				$str.= '<meta itemprop="position" content="2">';
      			$str.= '<a href="'. get_permalink($ancestor).'" itemprop="item"><span itemprop="name">'. get_the_title($ancestor) .'</span></a> &gt;&#160;</div>';
                }
            }	
        } 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.= '<div itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem" >';
					$str.= '<meta itemprop="position" content="2">';
      				$str.= '<a href="'. get_category_link($ancestor).'" itemprop="item"><span itemprop="name">'. get_cat_name($ancestor). '</span></a>→</div>';
                }
            }
			$str.= '<div itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem" >';
			$str.= '<meta itemprop="position" content="2">';
      		$str.= '<a href="'. get_category_link($cat -> term_id). '" itemprop="item"><span itemprop="name">'. $cat-> cat_name . '</span></a> &gt;&#160;</div>';		
        }
        $str.='<div>'. wp_title('', false) .'</div>';
    }
    echo $str;
}
?>

And add the following code in the page you show it. Add in block if you usee AdInserter.

<?php breadcrumb(); ?>

Finish! Complete!