Wordressの「タグ」をmetaタグのkeywordに自動的に埋め込ませるする方法。

Wordpressにプラグインなし+自動的にKeywordにタグを埋め込む方法。

MontBlanc.です。

前回の記事でAll In One SEO PACKを外したことが原因で、
metaタグのkeywordがページ内に表示されなくなったため、復活を試みました。

実装方法

keywordとdescriptionの自動取得になります。
以下をheader.phpのより前に挿入します。

<?php if ( is_single() ) { // エントリーページ ?>
    <meta name="keywords" content="<?php foreach((get_the_category()) as $category) { echo $category->cat_name . ','; } $posttags = get_the_tags();if ($posttags) {foreach($posttags as $tag) {echo $tag->name . ','; } } ?>" />
<?php if ($post->post_excerpt){ // 抜粋がある場合
    $summary = strip_tags($post->post_excerpt);
    $summary = ereg_replace("(\r\n|\r|\n)", "", $summary); ?>
    <meta name="description" content="<? echo $summary; ?>" />
<?php } else { // 抜粋がない場合の処理
    $content_summary = strip_tags($post->post_content);
    $content_summary = ereg_replace("(\r\n|\r|\n)", "", $content_summary);
    $content_summary = mb_substr($content_summary, 0, 60). "..."; ?>
    <meta name="description" content="<?php echo $content_summary; ?>" />
    <?php } ?>
<?php } else { // エントリーページ以外 ?>
    <meta name="keywords" content="デフォルトのキーワードを入れておく" />
    <meta name="description" content="<?php bloginfo('description'); ?>" />
    <?php } ?>
 

概要

・現在の投稿ページかどうかを取得
・ページ内のカテゴリとタグを取得して、keywordにカンマ付きで挿入
・抜粋があるかないかを取得
 └ある場合はdescriptionに抜粋内容を表示
 └ない場合は投稿内容を表示
・エントリーページでない場合は固定のキーワードとブログ概要文を表示

問題なく実装されると下記のようにきちんと表示されるように!b
スクリーンショット 2016-03-31 4.08.17

上記をコピペするだけさくっといけるので、ぜひお試しあれですb