2014年08月04日 web関連WordPressphpSEO対策

WordPressのパンくずリストをリッチスニペット(microdata)に対応させてみました。
リッチスニペットについては下記を参考にしてください。
リッチ スニペット – パンくずリスト(Google)

対応後のコードはGistにあります。という割に、Gistの使い方がよくわかっていません・・。 少しずつ勉強していきます。(^_^;)

microdata対応のWordpress用パンくずリスト(Gist)
<?php
function breadcrumb($divOption = array("id" => "breadcrumb", "class" => "clearfix")){
	global $post;
	$str ='';
	if(!is_home()&&!is_admin()){ /* !is_admin は管理ページ以外という条件分岐 */
		$tagAttribute = '';
		foreach($divOption as $attrName => $attrValue){
			$tagAttribute .= sprintf(' %s="%s"', $attrName, $attrValue);
		}
		$str.= '<div'. $tagAttribute .'>';
		$str.= '<ul>';
		$str.= '<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. home_url() .'/" itemprop="url"><span itemprop="title">ホーム</span></a></li>';
		$str.= '<li>&gt;</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 itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_category_link($ancestor) .'" itemprop="url"><span itemprop="title">'. get_cat_name($ancestor) .'</span></a></li>';
					$str.='<li>&gt;</li>';
				}
			}
			$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_category_link($cat -> cat_ID) .'" itemprop="url"><span itemprop="title">'. $cat -> name . '</span></a></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 itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_category_link($ancestor).'" itemprop="url"><span itemprop="title">'. get_cat_name($ancestor). '</span></a></li>';
					$str.='<li>&gt;</li>';
				}
			}
			$str.='<li 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></li>';
			$str.='<li>&gt;</li>';
			$str.= '<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'.get_permalink($post->ID).'" itemprop="url"><span itemprop="title">'. $post -> post_title .'</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 itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_permalink($ancestor).'" itemprop="url"><span itemprop="title">'. get_the_title($ancestor) .'</span></a></li>';
					$str.='<li>&gt;</li>';
				}
			}
			$str.= '<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'.get_permalink($post->ID).'" itemprop="url"><span itemprop="title">'. $post -> post_title .'</span></a></li>';
		} elseif(is_date()){							//日付ベースのアーカイブページ
			if(get_query_var('day') != 0){				//年別アーカイブ
				$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_year_link(get_query_var('year')). '" itemprop="url"><span itemprop="title">' . get_query_var('year'). '年</span></a></li>';
				$str.='<li>&gt;</li>';
				$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_month_link(get_query_var('year'), get_query_var('monthnum')). '" itemprop="url"><span itemprop="title">'. get_query_var('monthnum') .'月</span></a></li>';
				$str.='<li>&gt;</li>';
				$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_day_link(get_query_var('year'), get_query_var('monthnum'), get_query_var('day')) .'" itemprop="url"><span itemprop="title">'. get_query_var('day'). '日</span></a></li>';
			} elseif(get_query_var('monthnum') != 0){	//月別アーカイブ
				$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_year_link(get_query_var('year')) .'" itemprop="url"><span itemprop="title">'. get_query_var('year') .'年</span></a></li>';
				$str.='<li>&gt;</li>';
				$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_month_link(get_query_var('year'), get_query_var('monthnum')) .'" itemprop="url"><span itemprop="title">'. get_query_var('monthnum'). '月</span></a></li>';
			} else {									//年別アーカイブ
				$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_year_link(get_query_var('year')) .'" itemprop="url"><span itemprop="title">'. get_query_var('year') .'年</span></a></li>';
			}
		} elseif(is_search()) {							//検索結果表示ページ
			$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'.get_bloginfo('url').'/?'.$_SERVER['QUERY_STRING'].'" itemprop="url"><span itemprop="title">「'. get_search_query() .'」の検索結果</span></a></li>';
		} elseif(is_author()){							//投稿者のアーカイブページ
			$str .='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'.get_author_posts_url(get_query_var('author')).'" itemprop="url"><span itemprop="title">投稿者 : '. get_the_author_meta('display_name', get_query_var('author')).'</span></a></li>';
		} elseif(is_tag()){								//タグのアーカイブページ
			$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'.get_tag_link(get_query_var('tag_id')).'" itemprop="url"><span itemprop="title">タグ : '. single_tag_title( '' , false ). '</span></a></li>';
		} elseif(is_attachment()){						//添付ファイルページ
			$str.= '<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><span itemprop="title">'. $post -> post_title .'</span></li>';
		} elseif(is_404()){								//404 Not Found ページ
			$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><span itemprop="title">404 Not found</span></li>';
		} else{											//その他
			$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><span itemprop="title">'. wp_title('', true) .'</span></li>';
		}
		$str.='</ul>';
		$str.='</div>';
	}
	echo $str;
}
?>
コード参考元 : http://webdesignrecipes.com/wordpress-breadcrumb-list-tips/
コード参考元 : https://gist.github.com/wate/50a4186223f187128549

2014/08/12追記

上記のコードだとパーマリンクの設定をデフォルトで運用していると
ホーム>0年
のように表示されてしまいます。

0年と表示される問題を修正したバージョンは以下です。 修正版(Gist)
<?php


//パンくず
function breadcrumb($divOption = array("id" => "bread", "class" => "clear")){
	global $post;
	$str ='';
	if(!is_home()&&!is_admin()){ /* !is_admin は管理ページ以外という条件分岐 */
		$tagAttribute = '';
		foreach($divOption as $attrName => $attrValue){
			$tagAttribute .= sprintf(' %s="%s"', $attrName, $attrValue);
		}
		$str.= '<div'. $tagAttribute .'>';
		$str.= '<ul>';
		$str.= '<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. home_url() .'/" itemprop="url"><span itemprop="title">ホーム</span></a></li>';
		$str.= '<li>&gt;</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 itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_category_link($ancestor) .'" itemprop="url"><span itemprop="title">'. get_cat_name($ancestor) .'</span></a></li>';
					$str.='<li>&gt;</li>';
				}
			}
			$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_category_link($cat -> cat_ID) .'" itemprop="url"><span itemprop="title">'. $cat -> name . '</span></a></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 itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_category_link($ancestor).'" itemprop="url"><span itemprop="title">'. get_cat_name($ancestor). '</span></a></li>';
					$str.='<li>&gt;</li>';
				}
			}
			$str.='<li 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></li>';
			$str.='<li>&gt;</li>';
			$str.= '<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'.get_permalink($post->ID).'" itemprop="url"><span itemprop="title">'. $post -> post_title .'</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 itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_permalink($ancestor).'" itemprop="url"><span itemprop="title">'. get_the_title($ancestor) .'</span></a></li>';
					$str.='<li>&gt;</li>';
				}
			}
			$str.= '<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'.get_permalink($post->ID).'" itemprop="url"><span itemprop="title">'. $post -> post_title .'</span></a></li>';
		} elseif(is_date()){							//日付ベースのアーカイブページ
			if(is_day()){				//日別アーカイブ
				$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_year_link(get_the_time('Y')). '" itemprop="url"><span itemprop="title">' . get_the_time('Y'). '年</span></a></li>';
				$str.='<li>&gt;</li>';
				$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_month_link(get_the_time('Y'), get_the_time('m')). '" itemprop="url"><span itemprop="title">'. get_the_time('m') .'月</span></a></li>';
				$str.='<li>&gt;</li>';
				$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_day_link(get_the_time('Y'), get_the_time('m'), get_the_time('d')) .'" itemprop="url"><span itemprop="title">'. get_the_time('d'). '日</span></a></li>';
			} elseif(is_month()){	//月別アーカイブ
				$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_year_link(get_the_time('Y')) .'" itemprop="url"><span itemprop="title">'. get_the_time('Y') .'年</span></a></li>';
				$str.='<li>&gt;</li>';
				$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_month_link(get_the_time('Y'), get_the_time('m')) .'" itemprop="url"><span itemprop="title">'. get_the_time('m'). '月</span></a></li>';
			} else {									//年別アーカイブ
				$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_year_link(get_the_time('Y')) .'" itemprop="url"><span itemprop="title">'. get_the_time('Y') .'年</span></a></li>';
			}
		} elseif(is_search()) {							//検索結果表示ページ
			$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'.get_bloginfo('url').'/?'.$_SERVER['QUERY_STRING'].'" itemprop="url"><span itemprop="title">「'. get_search_query() .'」の検索結果</span></a></li>';
		} elseif(is_author()){							//投稿者のアーカイブページ
			$str .='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'.get_author_posts_url(get_query_var('author')).'" itemprop="url"><span itemprop="title">投稿者 : '. get_the_author_meta('display_name', get_query_var('author')).'</span></a></li>';
		} elseif(is_tag()){								//タグのアーカイブページ
			$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'.get_tag_link(get_query_var('tag_id')).'" itemprop="url"><span itemprop="title">タグ : '. single_tag_title( '' , false ). '</span></a></li>';
		} elseif(is_attachment()){						//添付ファイルページ
			$str.= '<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><span itemprop="title">'. $post -> post_title .'</span></li>';
		} elseif(is_404()){								//404 Not Found ページ
			$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><span itemprop="title">404 Not found</span></li>';
		} else{											//その他
			$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><span itemprop="title">'. wp_title('', true) .'</span></li>';
		}
		$str.='</ul>';
		$str.='</div>';
	}
	echo $str;
}


?>

この投稿へのコメント

コメントはまだありません。

ピックアップ記事

現在のURLを取得し、エンコードして、facebook いいね 、mixi いいね に渡す

2011年07月25日 php
facebook いいね 、mixi いいね を利用する際に渡すURLはエンコードしたものしかダメなので、現在のURLを取得し、それをurlencodeしてからfacebookやmixiのいいねボタンに設定します。 …
「現在のURLを取得し、エンコードして、facebook いいね 、mixi いいね に渡す」をはてなブックマークに追加

Dreamweaverのソース検索で正規表現を使う

2012年01月23日 web関連
今回は、wordpressからエクスポートした記事のxmlデータを、他のwordpressに入れる際に、xmlにカテゴリやタグが入っていると、新しいwordpressにもカテゴリが全部追加されて、不要なカテゴリが増えてしまったので、それを防ぐために、インポート前のxmlデータからカテゴリやタグにあたる箇所を削除してから、インポートするためにdreamweaverで正規表現を使いました。 Word…
「Dreamweaverのソース検索で正規表現を使う」をはてなブックマークに追加

Twitter Bootstrapでスマホ回転時の表示崩れ

2013年11月13日 WordPressjQuery・javascript
Twitter Bootstrapを使用して、レスポンシブなウェブサイトを制作していたのですが、iPhoneで縦画面、横画面での表示を繰り返すと画面からはみ出してしまう(拡大表示した時のように画面内にサイトが収まっていない)現象に悩まされました。 もちろん、縦でも横でも画面横幅ピッタリにサイトが表示されるのが理想なので、調べてみました。 今回使用していたのは、Bootstrap v3.…
「Twitter Bootstrapでスマホ回転時の表示崩れ」をはてなブックマークに追加

wordpress 現在のuser_idやdisplay_nameを取得

2020年01月14日 WordPress
現在のuser_idを取得 [php] $user_id = get_current_user_id();[/php] 現在のdisplay_nameを取得 [php]$user = wp_get_current_user(); $display_name = $user->display_name;[/php]
「wordpress 現在のuser_idやdisplay_nameを取得」をはてなブックマークに追加

有名サイトのロゴ フォント

2011年07月27日 web関連
Twitter …… Pico Alphabet (Modified)Digg …… FFF Forward (Modified)Last.fm …… National (Modified)Flickr …… Frutiger BlackYouTube …… Alternate Gothic No. 2Facebook …… Klavika (Modified)Engadget …… Engadg…
「有名サイトのロゴ フォント」をはてなブックマークに追加
© graffiti on the web . All rights reserved. WordPress Theme by comfy