WordPressで記事のタイトルタグにサイト名を表示させない

2020/08/07

(更新日:2023/10/19

WordPressで記事のタイトルタグにサイト名を表示させない

一般的にタイトルタグにはサイト名が入っている場合が多いです。

しかし、SEO的にサイト名が記事ページのタイトルに入るとノイズになる場合に、
タイトルを消したいときの処理。

合わせてハイフンつなぎをパイプつなぎに変更。

document_title_partsを使ってタイトル出力を変える

例)この記事のタイトルの場合

<title>WordPressでタイトルタグにサイト名を表示させない | YAMATO BLOG</title>
↓
<title>WordPressでタイトルタグにサイト名を表示させない</title>

今回は「is_single()」を使って個別記事ページのみの変更してみます。

functions.phpに以下コードを記述。

function custom_title( $title ){
  if(is_single()){
    unset($title['site']);
  }
  return $title;
}
add_filter( 'document_title_parts', 'custom_title', 10, 2 );

合わせてdocument_title_separatorを使ってハイフンつなぎをパイプに変更

function custom_sep($sep) {
  return ' | ';
}
add_filter('document_title_separator', 'custom_sep', 10, 1);

最終コード

/**
 * タイトルタグ変更
 */
function custom_title( $title ){
  if(is_single()){
    unset($title['site']);
  }
  return $title;
}
add_filter( 'document_title_parts', 'custom_title', 10, 2 );

function custom_sep($sep) {
  return ' | ';
}
add_filter('document_title_separator', 'custom_sep', 10, 1);