Created
August 13, 2014 18:59
-
-
Save apr20/afdbcaf378c86dd93641 to your computer and use it in GitHub Desktop.
WordPressのdescription設定
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//descriptionの設定 | |
function set_description(){ | |
global $post; | |
$content = $post->post_content; | |
//トップページ | |
if ( is_front_page() ) { | |
$desc = get_bloginfo('name').":".get_bloginfo('description'); | |
}else{ | |
if(is_page() or is_single()){ //固定ページ、記事ページ | |
//phpタグを除去 | |
$content = preg_replace("/<\?php[^>]+\>/i"," ",$content); | |
//htmlタグを除去 | |
$desc = wp_title('',false).":".strip_tags($content); | |
}elseif( is_category()){ //カテゴリページ | |
if(get_query_var('paged')){ | |
$desc = get_query_var('paged')."ページ目:"; | |
} | |
$desc .= wp_title('',false)."カテゴリの記事一覧"; | |
}elseif( is_archive()){ //アーカイブページ | |
if(is_date()){ //日付ベースのアーカイブページ | |
if(is_day()){ //日別アーカイブ | |
if(get_query_var('paged')){ | |
$desc = get_query_var('paged')."ページ目:"; | |
} | |
$desc.= get_the_time('Y'). '年'; | |
$desc.= get_the_time('m') .'月'; | |
$desc.= get_the_time('d'). '日'; | |
}elseif(is_month()){ //月別アーカイブ | |
if(get_query_var('paged')){ | |
$desc = get_query_var('paged')."ページ目:"; | |
} | |
$desc.= get_the_time('Y') .'年'; | |
$desc.= get_the_time('m'). '月'; | |
}else { //年別アーカイブ | |
if(get_query_var('paged')){ | |
$desc = get_query_var('paged')."ページ目:"; | |
} | |
$desc.= get_the_time('Y') .'年'; | |
} | |
}elseif(is_author()){ //投稿者アーカイブ | |
if(get_query_var('paged')){ | |
$desc = get_query_var('paged')."ページ目:"; | |
} | |
$desc .='[投稿者:'. get_the_author_meta('display_name', get_query_var('author'))."]"; | |
}elseif(is_tag()){ //タグアーカイブ | |
if(get_query_var('paged')){ | |
$desc = get_query_var('paged')."ページ目:"; | |
} | |
$desc.='[タグ:'. single_tag_title( '' , false )."]"; | |
}else{ | |
if(get_query_var('paged')){ | |
$desc = get_query_var('paged')."ページ目:"; | |
} | |
$desc .= wp_title('',false); | |
} | |
$desc .= "の記事一覧"; | |
}elseif(is_search()) { //検索結果 | |
if(get_query_var('paged')){ | |
$desc = get_query_var('paged')."ページ目:"; | |
} | |
$desc.= '['. get_search_query() .']の検索結果一覧'; | |
}else{ | |
if(get_query_var('paged')){ | |
$desc = get_query_var('paged')."ページ目:"; | |
} | |
$desc .= wp_title('',false); | |
} | |
//descriptionが短すぎる場合はサイト概要、サイト名追加 | |
if(mb_strlen($desc)<=30){ | |
$desc .= " ".get_bloginfo('description')."【".get_bloginfo('name')."】"; | |
} | |
//descriptionが64文字を超える場合の調整 | |
if(mb_strlen($desc)>64){ | |
$desc = mb_substr($desc,0,60)."..."; | |
} | |
} | |
//固定ページ、記事ページの本文抜粋の改行削除 | |
$desc = str_replace(array("\r\n","\r","\n"), '', $desc); | |
echo '<meta name="description" content="'.esc_html($desc).'">'; | |
} | |
//descriptionの設定 | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment