get_the_archive_title で勝手に追加される「アーカイブ:」などを削除

  • Updated: 2022.10.27
  • Published: 2022.10.27
  • 972views

概要

get_the_archive_title();

上記でアーカイブのタイトルを書き出したい時に勝手に「アーカイブ: ○○○○○」と表示されるのを毎回削除し方を忘れるので書きとめておきます。

フックで削除する

無理やり感はありますが、簡単でわかりやすかったので下記の方法で実装しました。

function change_archive_title($title){
	$titleArray = explode(': ',$title);
	if($titleArray[1]):
		$title = $titleArray[1];
	endif;
	return $title;
}
add_filter('get_the_archive_title','change_archive_title');

「アーカイブ: ○○○○○」の文字列をexplodeを使用して「: 」で分割し、右側だけを返すようにします。

template-tags.phpがある場合

template-tags.phpがある場合、テーマがタイトルを書き換えていることがあるので、ファイルを直接変更するか、書き換えている関数を無効にします。

テーマ「twentytwenty」の場合

	$regex = apply_filters(
		'twentytwenty_get_the_archive_title_regex',
		array(
			'pattern'     => '/(\A[^\:]+\:)/',
			'replacement' => '<span class="color-accent">$1</span>',
		)
	);

直接書き換える場合は「replacement」を変更、spanで囲うのをやめる


フックを無効にする場合

remove_filter( 'get_the_archive_title', 'twentytwenty_get_the_archive_title' );

変更している関数「twentytwenty_get_the_archive_title」を「remove_filter」で無効にする。

人気の投稿

最新の投稿

タグ

月別アーカイブ

Contact

WEB制作の依頼など気軽にお問い合わせください。

お問い合わせ