Written by Anonymous
// アーカイブタイトルの取得
if ( !function_exists( 'get_archive_chapter_title' ) ) :
function get_archive_chapter_title(){
$chapter_title = '';
if( is_category() ) { // カテゴリーページの場合
$icon_font = '<span class="fa fa-folder-open" aria-hidden="true"></span>';
$category = get_queried_object();
if ( $category && isset($category->name) ) {
$chapter_title .= $icon_font . esc_html($category->name);
} else {
$chapter_title .= single_cat_title($icon_font, false);
}
} elseif( is_tag() || is_tax()) { // タグ・タクソノミページの場合
$icon_font = '<span class="fa fa-tags" aria-hidden="true"></span>';
// 現在のタームのタクソノミーを取得
$tag = get_queried_object();
if ( is_tax() ) {
$taxonomy = $tag->taxonomy; // 現在のタクソノミーを取得
// 階層型タクソノミーの場合はフォルダアイコン、階層型でない場合はタグアイコン
if ( is_taxonomy_hierarchical($taxonomy) ) {
$icon_font = '<span class="fa fa-folder-open" aria-hidden="true"></span>';
} else {
$icon_font = '<span class="fa fa-tags" aria-hidden="true"></span>';
}
}
if ( $tag ) {
$chapter_title .= $icon_font . esc_html($tag->name);
} else {
$chapter_title .= single_tag_title($icon_font, false);
}
} elseif( is_search() ) { // 検索結果ページ
$search_query = trim(strip_tags(get_search_query()));
if (empty($search_query)) {
$search_query = __( 'キーワード指定なし', THEME_NAME ); // THEME_NAME を適切なテキストドメインに変更
}
$chapter_title .= '<span class="fa fa-search" aria-hidden="true"></span>"' . esc_html($search_query) . '"';
} elseif (is_day()) { // 日別アーカイブ
$chapter_title .= '<span class="fa fa-calendar" aria-hidden="true"></span>' . get_the_time('Y-m-d');
} elseif (is_month()) { // 月別アーカイブ
$chapter_title .= '<span class="fa fa-calendar" aria-hidden="true"></span>' . get_the_time('Y-m');
} elseif (is_year()) { // 年別アーカイブ
$chapter_title .= '<span class="fa fa-calendar" aria-hidden="true"></span>' . get_the_time('Y');
} elseif (is_author()) { // 著者ページ
$chapter_title .= '<span class="fa fa-user" aria-hidden="true"></span>' . esc_html(get_the_author());
} elseif ( is_post_type_archive() ) { // カスタム投稿タイプアーカイブ
$post_type = get_query_var('post_type');
$pt_obj = get_post_type_object($post_type);
if ( $pt_obj ) {
$chapter_title .= '<span class="fa fa-folder-open" aria-hidden="true"></span>' . esc_html( $pt_obj->labels->name );
} else {
$chapter_title .= 'Archives';
}
} elseif (is_paged()) { // 2ページ目以降
$chapter_title .= 'Archives';
} else { // その他
$chapter_title .= 'Archives';
}
return apply_filters('get_archive_chapter_title', $chapter_title);
}
endif;