Categories
WordPress WordPress Templates

WordPress: Filter Archive Title Output

If you need to output the title of an archive in a WordPress theme, you could use functions like single_cat_title(), single_tag_title(), etc.

The trouble, however, is that they’re limited to the type of archive (category, tag, etc) listed in the function name itself.

A more versatile approach is to use the_archive_title function. It will work on any archive file (such as archive.php), which means it can cut down on the number of template files we might otherwise need.

The usage is simple. For example, if you want to put the title of a category or tag archive inside a heading tag, you would use either of the two options below (in the archive.php template file).

The first simply nests the function call between the opening and closing of the heading element:

<H2 class="page-title"><?php the_archive_title(); ?></h2>

The second passes the opening and closing of the heading tags as values to the function (which then outputs the first tag before the title and the second tag after it):

<?php the_archive_title('<h2 class="page-title">', '</h2>'); ?>

The results are identical.

A potentially annoying thing with both, however, is that the_archive_title() appends the type of archive to the text output as our heading:

  • category: technology
  • tag: american politics
  • author: margaret atwood
  • date: may 16, 2019

To remove that word, you need to filter the output from the get_the_archive_title() function.

Filtering involves modifying the output of a function before it is output to the page. You will do this either in functions.php or by writing a plugin to do the same thing.

To use this code in your own theme, copy it into functions.php. If you’re using the underscores starter theme, I typically put this after the series of add_action calls. (At the time of this writing, the last add_action call in the underscores functions file handles the enqueuing of scripts and stylesheets).

function YOUR-THEME-SLUG-HERE_archive_title( $title ) {
    if ( is_category() ) {
        $title = single_cat_title( '', false );
    } elseif ( is_tag() ) {
        $title = single_tag_title( '', false );
    } elseif ( is_author() ) {
        $title = '<span class="vcard">' . get_the_author() . '</span>';
    } 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', 'YOUR-THEME-SLUG-HERE_archive_title' );

That code will strip the prefix word from category, tag, author, custom post type, and custom taxonomy archives.

Conversely, imagine that you want to change the title prefix, not just remove it. For a simple example, change the RETURN statement to the following:

return "Previous articles: <i>" . $title . "</i>";

(The dot operator converts elements to strings and concatenates them).

For another example, look at how the twentynineteen theme outputs archive titles:

To create this kind of output, we could modify how the $TITLE variable is constructed for each kind of archive:

 // MODIFY ARCHIVES TITLE

function YOUR_THEME_SLUG_archive_title( $title ) {
    if ( is_category() ) {
        $title = single_cat_title( '<span>Category Archive: </span>', false );
    } elseif ( is_tag() ) {
        $title = single_tag_title( '<span>Tag Archive: </span>', false );
    } elseif ( is_author() ) {
        $title = '<span>Author Archive: </span>' . get_the_author();
    } elseif ( is_post_type_archive() ) {
        $title = post_type_archive_title( '<span>Post Type Archive: </span>', false );
    } elseif ( is_tax() ) {
        $title = single_term_title( '<span>Taxonomy Archive: </span>', false );
    }

    return $title;
}

add_filter( 'get_the_archive_title', 'YOUR_THEME_SLUG_archive_title' );