Will McLean
Process diary

Disable the archive label in the default WordPress title function

Last modified: May 2, 2018
Created: March 19, 2018

When you are showing the title of an achive page in WordPress (ie. a taxonomy term, a post type achive etc.) You can’t use the_title(); as this just grabs the title of the first post in your page. You must use get_the_archive_title();. However this prepends the title with various forms of “Archive: “. So your title ends up being something like “Archive: Custom Post Types”. For me personally that is great, but I am a brutalist. I like to embrace the system with which we build our websites and display things like this with no alteration… But try getting a job with that attitude!! So below is how to remove them. Add this to your functions.php file. Source.


function remove_label_from_archive_title( $title ) {
    if ( is_category() ) {
        $title = single_cat_title( '', false );
    } elseif ( is_tag() ) {
        $title = single_tag_title( '', false );
    } elseif ( is_author() ) {
        $title = '' . get_the_author() . '';
    } elseif ( is_post_type_archive() ) {
        $title = post_type_archive_title( '', false );
    } elseif ( is_tax() ) {
        $title = single_term_title( '', false );
    }

    return $title;
}

add_filter( 'get_the_archive_title', 'remove_label_from_archive_title' );

This is a Knowledge Base post.