So I wonder why don’t you just set the category title via the admin page, e.g. for the one having the slug alphabetical
, you could set All Characters
as a static title for the category?
That way, you wouldn’t need to hook on get_the_archive_title
..
But anyway, here is one way of how can you reduce those (26) elseif
into just one: (using regular expression)
add_filter( 'get_the_archive_title', function ( $title ) {
if ( is_category( 'alphabetical' ) ) {
$title="All Characters";
// If the slug starts with <letter>-alphabetical, we set the title to
// "Characters Starting with <the LETTER in uppercase>".
} elseif ( is_category() && preg_match( '/^([a-z])-alphabetical$/', get_queried_object()->slug, $m ) ) {
$title="Characters Starting with " . strtoupper( $m[1] );
}
return $title;
} );
So in the above code, I used preg_match()
, and also get_queried_object()
which returns the term object/data of the current category on a category archive page (e.g. one at example.com/category/alphabetical/
).