Arthur

Pemberton

Full-stack web applications developer


Changing the “Archive Title” used by WordPress

July 21, 2015Arthur Pemberton0 Comments

If you are using an SEO plug-in with WordPress, you may have changed the <title> of one or more of your archives pages. However, the theme you are using is probably making use of the get_the_archive_title() WordPress function to get the title that is used in, for example, the <h1>.

By default, WordPress prefixes ‘Archives:’ with the post title:

sprintf( __( 'Archives: %s' ), post_type_archive_title( '', false ) );

You can change this title by adding a handler for the filter applied to the generated title.

function template_fix_title($title, $id = null) {
	if ( is_archive($id) && get_post_type($id) == 'event' ) {
		return 'Performances';
	}
	return $title;
}
add_filter( 'get_the_archive_title', 'template_fix_title', 10, 2 );

In this case, we verify that this is an archive page, and limit to a specific post type, ‘event‘, and do a complete substitution, with the string ‘Performances’. You can use what ever logic suits your need to manipulate the title, just be sure to return it.

Hope this helps you accomplish your website build.


Leave a Reply