4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【WordPress・プラグインなし】ページごとにmeta情報(keywords、description)を設定する方法

Last updated at Posted at 2019-03-23

デフォルトのWordpressにはSEOに重要なkeywordsやdescriptionが含まれていないため追加する必要がありますが、
header.phpに直書きで追加すると全ページ同じmeta情報が表示されてしまいます。

そこで、固定ページや記事ごとにカスタムフィールドを設置してmetaを設定する方法を紹介します。

まず、以下のコードをfunctions.phpに追加します。

functions.php
// keywords description
add_action('admin_menu', 'add_custom_fields');
add_action('save_post', 'save_custom_fields');

// 記事ページと固定ページでカスタムフィールドを表示
function add_custom_fields() {
  add_meta_box( 'my_sectionid', 'カスタムフィールド', 'my_custom_fields', 'post');
  add_meta_box( 'my_sectionid', 'カスタムフィールド', 'my_custom_fields', 'page');
}
function my_custom_fields() {
  global $post;
  $keywords = get_post_meta($post->ID,'keywords',true);
  $description = get_post_meta($post->ID,'description',true);
  echo '<p>キーワード(半角カンマ区切り)<br>';
  echo '<input type="text" name="keywords" value="'.esc_html($keywords).'" size="60"></p>';
  echo '<p>ページの説明(description)160文字以内<br>';
  echo '<input type="text" style="width: 600px;height: 40px;" name="description" value="'.esc_html($description).'" maxlength="160"></p>';
}

// カスタムフィールドの値を保存
function save_custom_fields( $post_id ) {
  if(!empty($_POST['keywords'])){
    update_post_meta($post_id, 'keywords', $_POST['keywords'] );
  }else{
    delete_post_meta($post_id, 'keywords');
  }
  if(!empty($_POST['description'])){
    update_post_meta($post_id, 'description', $_POST['description'] );
  }else{
    delete_post_meta($post_id, 'description');
  }
}
function MataTitle() {
  // カスタムフィールドの値を読み込む
  $custom = get_post_custom();
  if(!empty( $custom['keywords'][0])) {
    $keywords = $custom['keywords'][0];
  }
  if(!empty( $custom['description'][0])) {
    $description = $custom['description'][0];
  }
  if(is_home()){// トップページ
    echo '<meta name="keywords" content="'.$keywords.'">';
    echo '<meta name="description" content="'.$description.'">';
  }elseif(is_single()){// 記事ページ
    echo '<meta name="keywords" content="'.$keywords.'">';
    echo '<meta name="description" content="'.$description.'">';
  }elseif(is_page()){// 固定ページ
    echo '<meta name="keywords" content="'.$keywords.'">';
    echo '<meta name="description" content="'.$description.'">';
  }elseif(is_archive()){// 記事ページ
    echo '<meta name="keywords" content="'.$keywords.'">';
    echo '<meta name="description" content="'.$description.'">';
  }elseif(is_category()){// カテゴリーページ
    echo '<meta name="description" content="'.single_cat_title().'の記事一覧">';
  }elseif(is_tag()){// タグページ
    echo '<meta name="robots" content="noindex, follow">';
    echo '<meta name="description" content="'.single_tag_title("", true).'の記事一覧">';
  }elseif(is_404()){// 404ページ
    echo '<meta name="robots" content="noindex, follow">';
    echo '<title>404:お探しのページが見つかりませんでした</title>';
  }else{// その他ページ
    echo '<meta name="robots" content="noindex, follow">';
  };
}

次に、header.phpなどmetaを追加したい場所に作成した関数を設置します。

header.php
<?php MataTitle(); ?>
4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?