WordPressで自動でmetaタグのkeywordsとdescriptionを表示
2024/05/22
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);
参考サイト
関連記事
-
-
youtubeの検索結果をWordPressに自動投稿する方法
youtubeから特定のキーワードで検索した動画をWordPressに自動で投稿 ...
-
-
Polylangのスイッチャーとページャーの組み合わせ方法
WordPressで多言語サイトを作成するにあたりPolylangというプラグイ ...
-
-
WordPressでカテゴリに優先順位を決め複数カテゴリが設定されている記事の場合に優先順位の高いカテゴリの記事のみ取得
WordPressで精度の高い関連記事を表示させたい。色々試したところテーマファ ...
-
-
WordPressのContact Form 7プラグインで入力内容確認及び送信完了ページを実装する方法
WordPressのContact Form 7プラグインで実装されたお問い合わ ...
-
-
Contact form7でフォーム送信時に別サーバへもデータを送信する方法
contact form7でフォーム送信時に別サーバーでフォーム内容をPOST送 ...