Greg's Blog

moon indicating dark mode
sun indicating light mode

Filter and clean the Archive Title

April 13, 2016

This client wanted to feature their archive titles in a large hero area. The PSD excluded the prefixes like, “Category:”, “Tag:”, “Author:”, etc…

The snippet below allows you to strip out the prefixes used in the get_the_archive_title(); function, as well as any HTML tags.

Screenshot of the clean archive title

Screenshot of the “clean” archive title.

Jeremy Pry helped me solve this by inserting this regular expression into PHP’s preg_replace();  function. It executes a find and replace on the $title variable.

* Remove archive title prefixes.
* @param string $title The archive title from get_the_archive_title();
* @return string The cleaned title.
function grd_custom_archive_title( $title ) {
// Remove any HTML, words, digits, and spaces before the title.
return preg_replace( '#^[\w\d\s]+:\s*#', '', strip_tags( $title ) );
add_filter( 'get_the_archive_title', 'grd_custom_archive_title' );

Director of Engineering @WebDevStudios / Author & Editor @WileyGlobal. Read more about me.