WordPressで自動でmetaタグのkeywordsとdescriptionを表示
metaタグのkeywordsとdescriptionにて、基本的にはAll in One SEOとかで手動で設定していくべきなんだろうけど、記事数勝負のコンテンツとか自動生成系のコンテンツだとどうにもならないので、自動で表示する方法をメモ。
PC側テーマのヘッダー部分に下記ソースを記述。
<!--description&keywords--> <meta name="description" content="<?php if(is_home()){ ?>トップページに表示したいdescriptionを書く<?php } elseif (is_single()){ $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). "..."; $content_summary=strip_tags($content_summary); echo $content_summary; } else { ?><?php bloginfo('description'); ?><?php } ?>"> <meta name="keywords" content="<?php if(is_home()) { ?>トップページに設定したいキーワード,キーワード①,キーワード②<?php } elseif (is_single()){ ?><?php $posttags = get_the_tags();if ($posttags) {foreach($posttags as $tag) {echo $tag->name . ','; }} ?><?php the_title(); ?><?php } else {?>デフォルトのキーワード①,デフォルトのキーワード②<?php } ?>"> <!--description&keywords-->
あとは必要に応じて、
$content_summary = ereg_replace("\"", "", $content_summary);
とかで修正していく。
参考サイト
関連記事
Advanced Custom FieldsでGoogleMapの使用する方法
Advanced Custom FieldsでGoogleMapを使用したかった ...
WordPressのcontact form7で500エラーの対応方法
WordPressプラグインのcontact form7で送信ボタンを押し、ロー ...
WordPress内でPHPの独自プログラムページを実装する方法
WordPress内にてPHPで作ったシステムを実装したい。ヘッダー / フッタ ...
WordPressで画像にカテゴリを設定し、記事投稿の際の画像選択時に指定したカテゴリの画像のみ表示させる方法
WordPressのメディアライブラリに画像を沢山登録していると、記事投稿の際の ...
WordPressにて記事内で動画(mp4)ファイルの再生する方法
WordPressでyoutube等の動画サイトの埋め込みタグではなく、mp4フ ...