How to remove taxonomy name from WordPress archive title

Always remember! When you want to modify a piece of data that WordPress is generating for us automatically, we should be able to edit that data using the WordPress Filter Hooks.

The Archive page titles are being generated by WordPress automatically based on the type of archive that we are currently on. 

And we should be able to play around and modify the Archive page titles using the get_the_archive_title filter hook.

So, go ahead and place the following code at the end of the functions.php file:


/**
 * Remove default words from archive titles like "Category:", "Tag:", "Archives:"
 */
function nd_dosth_remove_default_archive_words($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>' ;
    }
    return $title;
}
add_filter( 'get_the_archive_title', 'nd_dosth_remove_default_archive_words');

In the above code, first, we are detecting if it is a category archive using the is_category() conditional tag. If it is, we are returning just the name of the category using the single_cat_title() function.

If it is a tag archive, then we are using the single_tag_title() function.

If it is an author archive, we are returning just the name of the author using the get_the_author() function.

If we now go ahead and check out the default archive pages, for example:

http://localhost:8888/dosth/category/advice/

You would just see the category name:

Of course, we would have totally eliminated this step if we would have outputted the archive titles inside archive.php file like this:

<?php
/**
 * The template for displaying archive pages
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package Dosth
 */
get_header();
?>
<div class="content-container">
    <h1 class="page-title">
        <?php
            if ( is_category() ) {
                echo single_cat_title( '', false );
        
            } elseif ( is_tag() ) {
        
                echo single_tag_title( '', false );
        
            } elseif ( is_author() ) {
        
                echo '<span class="vcard">' . get_the_author() . '</span>' ;
        
            } else {
                the_archive_title();
            }
        ?>

    </h1>    
    <div class="container">
        <div class="row">
            <div class="blog-posts col-md-8">
            <?php if ( have_posts() ): ?>
                <?php while( have_posts() ): ?>
                    <?php the_post(); ?>
                    <?php get_template_part( 'parts/blog', 'index' ); ?>
                <?php endwhile; ?>
                <?php the_posts_pagination(); ?>
            <?php else: ?>
                <p><?php _e( 'No Blog Posts found', 'nd_dosth' ); ?></p>
            <?php endif; ?>
            </div>
            <div id="blog-sidebar" class="col-md-4">
                <?php get_sidebar(); ?>                  
            </div>
        </div>
    </div>
</div>
<?php get_footer(); ?>

WordPress conditional tags work everywhere. Inside every PHP file that belongs to our theme.

At the end of the day, Its all about personal preference. 

Most of my clients prefer to keep the default words in the archive titles. Simply put, it varies from client to client.

Anyway, in the next lesson, we will how to create a custom taxonomy.