この記事ではthe_titleとpre_get_document_titleの違いについて紹介します。
ざっくり言うと?
最初に結論をざっくり言うと、以下のようになります。- the_title: タイトルの表示に対応したフィルター
- pre_get_document_title: TITLEタグに対応したフィルター
各フィルターの詳しい例は次以降のセクションで紹介します。
the_titleの例
the_titleのフィルターを使用したサンプルコードは以下です。function AddCategoryToTitle_replaceTitle($title) {
if(is_singular() && in_the_loop()) {
if(count(get_the_category()) > 0) return $title . " | " . get_the_category()[0]->name;
else return $title;
}
return $title;
}
add_filter('the_title','AddCategoryToTitle_replaceTitle');
このコードは以下の記事で紹介しているものですが、表示するタイトルにカテゴリ名を付与するものになっています。
このコードを適用したときのタイトルは下図のようになっています。

pre_get_document_titleの例
pre_get_document_titleを使用したサンプルコードは以下です。function AddCategoryToTitle_replaceTitleTag($title) {
if(is_singular()) {
if(count(get_the_category()) > 0) return $title . " | " . get_the_category()[0]->name;
else return $title;
}
return $title;
}
add_filter('pre_get_document_title', 'AddCategoryToTitle_replaceTitleTag');
このコードもさきほどと同じ記事で紹介したもので、TITLEタグのタイトルにカテゴリ名を付与するものになっています。
このコードを適用したときのタイトルは下図のようになっています。
