Greg Rickaby

Greg Rickaby

Full-Stack Developer / Photographer / Author

Filter and clean the Archive Title

Posted on | 1 minute read

Showstoppers - Biloxi, MS

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.

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.

<?php
/**
 * 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' );

Comments

No comments yet.

MaquiJacob

MaquiJacob

Thank you! Very helpful!

Leave a Comment