meta keywordsにカテゴリーを自動設定する方法
<meta name="keywords" content="" />は設定しておくと検索エンジンにサイトのキーワードを知らせることができます。
SEO効果は殆ど無いとも全く無いともいわれていますが、念のため設定しておいて損は無いと思います。
実際には、<meta name="keywords" content="" />を表示してもアクセス数が上昇することも下降することもありませんでした。やはり、タイトルや記事内容が大切です。
また、一つ一つ<meta name="keywords" content="" />を記入するのも大変です。そのため、個別ページであれば記事に該当するカテゴリーを自動的にキーワードに設定します。
トップページやカテゴリーページ、タグページも自動で表示されるようにすると便利です。
このページでは、<meta name="keywords" content="" />にカテゴリーを設定する方法を紹介します。
トップページの設定
トップページの<meta name="description" content="" />は、サイトの概要が自動で表示されます。
<meta name="keywords" content="" />は、サイトのカテゴリーの一覧が最大5つまで自動で表示されます。
以下のソースコードを、header.phpに記述します。
<?php if( is_home() ): //トップページ用のメタデータ ?>
<meta name="description" content="<?php bloginfo( 'description' ); ?>" />
<?php $allcats = get_categories(array(
'number' => 5,
));
foreach($allcats as $allcat) {
$kwds[] = $allcat->name;
} ?>
<meta name="keywords" content="<?php echo implode( ',', $kwds ); ?>" />
<?php endif; //トップページ用のメタデータ【ここまで】?>
カテゴリー・タグページの設定
カテゴリー・タグページの<meta name="description" content="" />は、 カテゴリーページであれば、カテゴリー名+「に関する記事の一覧です。」と自動で表示されます。
また、タグページであれば、タグ+「に関する記事の一覧です。」と自動で表示されます。
<meta name="keywords" content="" />では、カテゴリー名やタグ名が自動で表示されます。
以下のソースコードを、header.phpに記述します。
<?php if( is_category() || is_tag() ): //カテゴリー・タグページ用のメタデータ ?>
<?php if( is_category() ) {
$termid = $cat;
$taxname = 'category';
} elseif( is_tag() ) {
$termid = $tag_id;
$taxname = 'post_tag';
} ?>
<meta name="description" content="<?php single_term_title(); ?>に関する記事の一覧です。" />
<?php $childcats = get_categories( array( 'child_of' => $termid ) );
$kwds = array();
$kwds[] = single_term_title('', false);
foreach($childcats as $childcat) {
$kwds[] = $childcat->name;
} ?>
<meta name="keywords" content="<?php echo implode( ',', $kwds ); ?>" />
<?php endif; //カテゴリー・タグページ用のメタデータ【ここまで】?>
記事ページの設定
カテゴリーをkeywordsに設定
個別の記事ページの設定<meta name="description" content="" />は、 記事内の最初の100文字が自動で表示されます。
<meta name="keywords" content="" />では、所属するカテゴリー名が自動で表示されます。複数カテゴリが設定されている場合は、複数表示されます。
以下のソースコードを、header.phpに記述します。
<?php if( is_single() || is_page() ): //記事の個別ページ用のメタデータ ?>
<meta name="description" content="<?php echo wp_trim_words( $post->post_content, 100, '…' ); ?>" />
<?php if( has_category() ): ?>
<?php $tags = get_the_category();
$kwds = array();
foreach($tags as $tag) {
$kwds[] = $tag->name;
} ?>
<meta name="keywords" content="<?php echo implode( ',', $kwds ); ?>" />
<?php endif; ?>
<?php endif; //記事の個別ページ用のメタデータ【ここまで】?>
タグをkeywordsに設定
もし、記事に設定したタグを<meta name="keywords" content="" />に設定したい場合は以下の部分を変更します。
変更前
<?php $tags = get_the_category();
変更後
<?php $tags = get_the_tags();
なお、<meta name="keywords" content="" />に設定するためにタグを量産すると、タグページが多くなります。
タグページを乱立させると、SEO的に良くありません。内容のないタグページが沢山量産されると検索順位が下降する場合があります。注意してください。
手動でkeywordsに設定
自分でキーワードを一つ一つ入力して表示させる方法もあります。この場合カスタムフィールドを使用して、<meta name="keywords" content="" />を設定していきます。
1.まずは、以下のソースコードを、header.phpに記述します。
<?php if( is_single() || is_page() ): //記事の個別ページ用のメタデータ ?> <meta name="description" content="<?php echo wp_trim_words( $post->post_content, 100, '…' ); ?>" /> <meta name="keywords" content="<?php echo $post->meta_keywords; ?>" /> <?php endif; //記事の個別ページ用のメタデータ【ここまで】?>
2.投稿記事にカスタムフィールドを表示させます。

名前に「meta_keywords」を記述します。
値に設定したいキーワードを記述します。複数記述する場合は「,」で区切ってください。
記述を終えればカスタムフィールドを追加します。
これで記事の<meta name="keywords" content="" />が設定できます。
まとめ
メタタグのキーワードはプラグインで設定する方法もあります。
しかし、簡単に設定ができる<meta name="keywords" content="" />はなるべくプラグインを使わずに、自分で設定する方が良いと思います。
PR:プラグインの広告
WordPressプラグインの「Meta Manager」を使用して、<meta name="description" content="" />や<meta name="keywords" content="" />を表示させる方法もあります。
過去のこの手のプラグインを使用していましたが、WordPressのバージョンアップなどでエラーが起きたり不具合が起きることもありますのでご了承ください。
*なお、プラグインなしでメタタグのキーワードを設定できた場合は「Meta Manager」は必要ありません。
レビュー
- 非常に使いやすいです。
- 無面倒でポイントにストレート。
- 非常に便利なプラグイン。
- すべてにお勧め。
この偉大なWordPressのプラグインの開発者に感謝します。
私にとってこのプラグインは完璧です。私はこれがインストール出来て喜んでいます。
このプラグインは、全てのページにメタタグを追加します。個別ページごとに異なるメタタグを追加すると上手くいきませんでした。プラグインの改善の余地がありそうです。
*Power by wordpress.org
コメント
一部コードに不具合がありました。’が大文字の’になっており、訂正いたしました。お詫び申し上げます。