In my child theme’s archive.php
, I have the following code for displaying the title of my archive pages:
<?php
the_archive_title( '<h1 class="page-title">', '</h1>' );
?>
But that displays my titles as “Category: Category Title” instead of simply the title without the prepended “Category: “.
My first instinct was to override get_the_archive_title()
from wp-includes/general-template
. But from what I’ve read, apparently I’m not supposed to ever alter wordpress core stuff, even with overrides from a child theme.
So what is the best-practice way to control the output of the_archive_title()
?
If you look at the source code of get_the_archive_title()
, you will see that there is a filter supplied, called get_the_archive_title
, through which you can filter the output from the function.
You can use the following to change the output on a category page
add_filter( 'get_the_archive_title', function ( $title ) {
if( is_category() ) {
$title = single_cat_title( '', false );
}
return $title;
});