ページ毎に異なるmeta descriptionを出力する
公開日:
更新日:
WordPressで、ページ毎に異なるmeta descriptionを出力する方法をご紹介します。
これは、add-meta-tags日本語化の作業を行っていたのですが、この部分は独自に作成した方が良いとの判断で、開発中のプラグインに使用するためのプログラムの一部です。
meta descriptionだけに限らず、好きな所でこのファンクションを呼び出すことで、ページ本文のテキストの先頭n文字を取得することができます。
WordPress標準では、このような関数はありませんし、いろいろ出回っているmeta出力用のプラグインでも複数ページにわたる記事やカテゴリアーカイブなどがサポートされていません。
このため、独自に開発したというものです。
では、さっそくサンプルコードをご紹介します。
function outMetaDescription($descLength = 80){
global $page;
if(is_home()){
// TOPページ
$mycontent = get_bloginfo('description');
if(!empty($mycontent)){
// mycontent変数からhtmlタグやwordpressタグなどを取り除く
$mycontent = cleanDescription($mycontent);
}else{
// mycontentがない場合はブログ名からhtmlタグやwordpressタグなどを取り除く
$mycontent = cleanDescription(get_bloginfo('name'));
}
if(!empty($mycontent)){
$descText = $mycontent;
if(is_paged()){
global $paged;
$descText .= ' (' . $paged . 'ページ)';
}
}
}elseif(is_search()){
// 検索結果ページ(検索キーワード)
$mycontent = cleanDescription(get_search_query());
if(!empty($mycontent)){
$descText = '「' . $mycontent . '」で' . get_bloginfo('name') . 'を検索した結果の一覧ページです。';
if(is_paged()){
global $paged;
$descText .= ' (' . $paged . 'ページ)';
}
}
}elseif(is_date()){
// アーカイブページ(YYYY年MM月アーカイブ)
$date = single_month_title('',FALSE);
$mycontent = ereg_replace('^[0-9]+.?月','',$date).'年'.ereg_replace('[0-9]+$','',$date);
if(!empty($mycontent)){
$mycontent = cleanDescription($mycontent); // mycontent変数からhtmlタグやwordpressタグなどを取り除く
$descText = $mycontent . 'の' . get_bloginfo('name') . '記事一覧ページです。';
if(is_paged()){
global $paged;
$descText .= ' (' . $paged . 'ページ)';
}
}
}elseif(is_singular()){
// 記事またはページ、添付ファイルページ
global $post;
$usePageOne = false; // 複数ページ目にも1ページ目の概要を使用するか
$useSplitFunc = false; // 概要を指定された文字数で切断すべきか
if(empty($post->post_content)){
// エントリーがそもそも空の場合
if(has_excerpt()){
// 手書きの概要がある場合
$mycontent = $post->post_excerpt;
}
}elseif(!empty($page) && $page != 1){
// 2ページ目以降
$mycontentAry = explode('<!--nextpage-->',$post->post_content);
if(empty($mycontentAry[$page-1])){
// 該当ページの中身が空の場合
$usePageOne = true;
}else{
// 該当ページの中身がある場合
$mycontent = $mycontentAry[$page-1];
$useSplitFunc = true;
}
}
if(!empty($post->post_content) && empty($mycontent)){
// mycontent変数がセットされていない(=1ページ目か、2ページ目以降も1ページ目の概要を使用)の場合
if(has_excerpt()){
// 手書きの概要がある場合
$mycontent = $post->post_excerpt;
}else{
// 手書きの概要がない場合
$mycontent = $post->post_content;
$useSplitFunc = true;
}
}
if($descLength == null || !ereg("[0-9]+",$descLength)){ $descLength = 80; }
// 長さ指定にnullが設定されている場合や数値以外が指定された場合は80文字目まで
$mycontent = cleanDescription($mycontent); // mycontent変数からhtmlタグやwordpressタグなどを取り除く
if(!empty($mycontent)){
if($useSplitFunc){
$descText = mb_substr($mycontent, 0, $descLength);
if($descText != $mycontent){ $descText .= '…'; }
}
}else{
$descText = wp_title('',false);
$descText = cleanDescription($descText); // descText変数からhtmlタグやwordpressタグなどを取り除く
$usePageOne = true;
}
if($usePageOne && !empty($descText) && !empty($page) && $page != 1){ $descText .= ' ('.$page.'ページ)'; } // 2ページ目以降で1ページ目の概要を使う場合
}elseif(is_author()){
// 投稿者ページ
$mycontent = wp_title('',false);
if(!empty($mycontent)){
$mycontent = cleanDescription($mycontent); // mycontent変数からhtmlタグやwordpressタグなどを取り除く
$descText = $mycontent . 'が書いた' . get_bloginfo('name') . 'の記事一覧ページです。';
if(is_paged()){
global $paged;
$descText .= ' (' . $paged . 'ページ)';
}
}
}else{
$mycontent = wp_title('',false);
if(!empty($mycontent)){
$mycontent = cleanDescription($mycontent); // mycontent変数からhtmlタグやwordpressタグなどを取り除く
$descText = $mycontent . 'に関する' . get_bloginfo('name') . 'の記事一覧ページです。';
if(is_paged()){
global $paged;
$descText .= ' (' . $paged . 'ページ)';
}
}
}
return($descText);
}
function cleanDescription($desc) {
$desc = stripslashes($desc);
$desc = strip_tags($desc);
if(is_singular()){ $desc = ereg_replace('[([^]]+)]','',$desc); }
$desc = htmlspecialchars($desc);
$desc = preg_replace('/([n tr]+)/', ' ', $desc);
if(!is_search()){ $desc = preg_replace('/( +)/', ' ', $desc); }
return trim($desc);
}
呼び出す際は、outMetaDescription(文字数)となります。
この関数は、概要テキストを返しますので、metaに埋め込むなどして利用します。
例:
$desc = outMetaDescription(80);
if(!empty($desc)){ echo '<meta name="description" content="' . $desc . '" />'; }
なお、サンプルコード下部のcleanDescriptionは、outMetaDescriptionの中から呼び出され、不要なWordPressのショーコードタグやhtmlタグを取り除くための関数です。