the_archive_titleの「:」より前の文字をシンプルに消す方法

the_archive_titleは、get_the_archive_title関数の戻り値の前後にコンテンツを追加して出力するシンプルな関数です。

function the_archive_title( $before = '', $after = '' ) {
	$title = get_the_archive_title();

	if ( ! empty( $title ) ) {
		echo $before . $title . $after;
	}
}

get_the_archive_title関数の定義元を確認してみます。

function get_the_archive_title() {
	$title  = __( 'Archives' );
	$prefix = '';

	if ( is_category() ) {
		$title  = single_cat_title( '', false );
		$prefix = _x( 'Category:', 'category archive title prefix' );
	} elseif ( is_tag() ) {
		$title  = single_tag_title( '', false );
		$prefix = _x( 'Tag:', 'tag archive title prefix' );
	}

/* (中略) */

	/**
	 * Filters the archive title prefix.
	 *
	 * @since 5.5.0
	 *
	 * @param string $prefix Archive title prefix.
	 */
	$prefix = apply_filters( 'get_the_archive_title_prefix', $prefix );
	if ( $prefix ) {
		$title = sprintf(
			/* translators: 1: Title prefix. 2: Title. */
			_x( '%1$s %2$s', 'archive title' ),
			$prefix,
			'<span>' . $title . '</span>'
		);
	}

/* (中略) */

}

バージョン5.5.0より、接頭辞のフィルターフックが追加されていることが分かりました。
空文字を返すコールバック関数を追加すると、「:」より前の文字を取り除くことができます。

add_filter('get_the_archive_title_prefix', function( $prefix ) {
  return '';
}); 

コメント