WEBTODESIGN

アーカイブのタイトルを変える【WordPress】

WordPressで、アーカイブのタイトルを変えるメモ。

functions.php

例えば

function.phpに以下を追記します。

///// アーカイブタイトルの書き換え /////
function custom_get_the_archive_title($title) {
    if(is_category(['news'])){
        $title = 'お知らせ';
    }
    return $title;
}
add_filter( 'get_the_archive_title', 'custom_get_the_archive_title'); 

この場合、「news」カテゴリーのアーカイブタイトルが「お知らせ」になります。

よく使う分岐

よく使う分岐の仕方は以下です。

  • カテゴリーアーカイブ(カテゴリー問わず)の場合はis_category()
  • 特定のカテゴリーの場合はis_category(['slug'])
  • 年別アーカイブの場合はis_year()
  • 投稿一覧ページの場合はis_post_type_archive()
///// アーカイブタイトルの書き換え /////
function custom_get_the_archive_title($title) {
    if(is_category(['slug'])){
        $title = 'タイトル';
    } elseif(is_category()){
        $title = single_cat_title( '', false );
    } elseif(is_year()){
        $title = get_the_date( _x( 'Y', 'yearly archives date format' ) );
    } elseif(is_post_type_archive()){
        $title = post_type_archive_title( '', false );
    }
    return $title;
}
add_filter( 'get_the_archive_title', 'custom_get_the_archive_title'); 

テンプレートタグ

アーカイブタイトルをテンプレートで取得するテンプレートタグは以下。

<?php echo get_the_archive_title(); ?>

functions.phpで設定したテキストが出力されます。

ソース

get_the_archive_title() | Function | WordPress Developer Resources