Last updated on September 13, 2023

Change Archive Page Title and Prefix

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Change the archive page title/prefix.

Modify the archive title prefix in WordPress

By default, WordPress adds the word “Archive” as a prefix to the title of archive pages, such as category archives, tag archives, and date archives. Here’s how you can change the archive title prefix:

function wpsnippets_archive_title_prefix( $prefix ) {

    if ( is_category() ) {
        $prefix = 'Genre';
    } elseif ( is_tag() ) {
        $prefix = 'Topic';
    } elseif ( is_date() ) {
        $prefix = 'Posted on';
    } elseif ( is_author() ) {
        $prefix = 'All posts by';
    } elseif ( is_post_type_archive( 'book' ) ) {
        $prefix = 'All books';
    }

    return $prefix;
}

add_filter('get_the_archive_title_prefix', 'wpsnippets_archive_title_prefix');

This code uses the get_the_archive_title_prefix filter to modify the archive title prefix. It checks the type of archive page being displayed (category, tag, date, author, or custom post type archive) and sets the prefix accordingly. You can modify the code to add your own custom archive types if needed.

Besides the prefix, the WordPress archive title itself displays the date range or term name for the current archive page. Here’s how you can change the archive title to a custom text:

function wpsnippets_archive_title( $title, $original_title, $prefix ) {

   // Check for taxonomy archive pages
   if ( is_category() || is_tag() || is_tax() ) {

      // Replace the ':' separator for '|'
      $prefix = str_replace( ':', ' | ', $prefix );

      // Add HTML wrappers
      $title  = '<span class="prefix">' . $prefix . '</span><span class="term-name">' . single_cat_title( '', false ) . '</span>';
   }

    return $title;
}

add_filter( 'get_the_archive_title', 'wpsnippets_archive_title', 10, 3);

That’s it! Now, when you view an archive page on the frontend, the archive title will display the custom text you specified.

Last updated on September 13, 2023. Originally posted on May 5, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *