LoginSignup
2
5

More than 3 years have passed since last update.

yoast seo をカスタマイズするちょっとした方法

Posted at

wordpressサイトの構築において、SEO系のプラグインを使用する場合、
代表的なものとして、「Yoast SEO」「All in One SEO Pack」があげられるのではないでしょうか。

Yoast SEOを使用した場合に、「個別ページ」「投稿ページ」「アーカイブページ」など、meta設定やrobotsの設定を一括で指定するができます。
また、それそれの編集ページで個別に調整することもできます。

しかし、個別に調整したい投稿が、全投稿のうち10件あったとした場合(10件の調整内容は同じとする)、10件それぞれに管理画面から編集していてはめんどくさい。
調べたら、フィルターをかましたら、諸々PHPで上書きできるようでした。管理的にもこちらのほうがよさそうなのかも〜。
メモがてらに、いくつか書き留めておきます。

メタタグ

title

add_filter( 'wpseo_title', 'custom_title' );
function custom_title($title) {
  //条件分岐
  if ( is_page('about') ) {
    return '会社概要';
  }
  return $title;
}

description

add_filter( 'wpseo_metadesc', 'custom_metadesc' );
function custom_metadesc($metadesc) {
  //条件分岐
  if ( is_page('about') ) {
    return '会社概要をご紹介いたします。';
  }
  return $metadesc;
}

robots

add_filter( 'wpseo_robots', 'custom_robots' );
function custom_robots($robots) {
  //条件分岐
  if ( is_single('test') ) {
    return 'noindex,nofollow';
  }
  return $robots;
}

OGタグ

og_site_name

add_filter( 'wpseo_opengraph_site_name', 'custom_og_site_name' );
function custom_og_site_name($site_name) {
  //条件分岐
  if ( is_page('about') ) {
    return '企業サイト';
  }
  return $site_name;
}

og_site_title

add_filter( 'wpseo_opengraph_title', 'custom_og_title' );
function custom_og_title($title) {
  //条件分岐
  if ( is_page('about') ) {
    return '会社概要';
  }
  return $title;
}

og_site_desc

add_filter( 'wpseo_opengraph_desc', 'custom_og_desc' );
function custom_og_desc($desc) {
  //条件分岐
  if ( is_page('about') ) {
    return '会社概要をご紹介いたします。';
  }
  return $desc;
}

参照サイト

2
5
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
2
5