How to remove Category: and Author: prefix titles

Posted on

How to remove Category: and Author: prefix titles

WordPress prefix titles are labels added before certain titles to provide context or categorize content. In WordPress, common prefix titles include "Category:" and "Author:", which are typically displayed before the titles of category archive pages and author archive pages, respectively.

For instance, on a category archive page displaying posts from a specific category, WordPress may display the title as "Category: [Category Name]", where "[Category Name]" is the name of the category. Similarly, on an author archive page showcasing posts by a particular author, the title might appear as "Author: [Author Name]", with "[Author Name]" representing the name of the author.

These prefix titles help visitors understand the context of the content they are viewing, making it easier to navigate and explore the website. However, in some cases, website owners may prefer to remove these prefixes for aesthetic reasons or to streamline the presentation of their content. This can be achieved through customizations using PHP functions and WordPress filters.

To remove the "Category:" and "Author:" prefixes from WordPress titles using a custom function, you can utilize WordPress hooks. Specifically, you'll want to hook into the get_the_archive_title filter to modify the archive titles (like category and author pages). Here's a step-by-step guide:

  1. Create a Child Theme or Use a Custom Plugin:
    If you're not already using a child theme, it's recommended to create one to add custom functionality without modifying the parent theme's files directly. Alternatively, you can use a custom plugin.

  2. Open your Child Theme's functions.php or Create a New Plugin File:
    You'll need to add your custom PHP function to either your child theme's functions.php file or a custom plugin file.

  3. Add the Custom PHP Function:
    Below is a PHP function that removes the "Category:" and "Author:" prefixes from archive titles. Add this function to your child theme's functions.php file or your custom plugin file:

function custom_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>';
    }
    return $title;
}
add_filter('get_the_archive_title', 'custom_archive_title');
  1. Explanation of the Function:

    • This function checks if the current page is a category archive, tag archive, or author archive.
    • If it's a category archive, it replaces the title with the single category title without the "Category:" prefix.
    • If it's a tag archive, it replaces the title with the single tag title without the "Tag:" prefix.
    • If it's an author archive, it replaces the title with the author's name without the "Author:" prefix.
  2. Save the File and Test:
    After adding the function to your child theme's functions.php file or your custom plugin file, save the changes and refresh your WordPress site to see the updated archive titles without the "Category:" and "Author:" prefixes.

By following these steps and adding the provided PHP function to your child theme or custom plugin, you should be able to remove the "Category:" and "Author:" prefixes from WordPress archive titles. Remember to always backup your files before making changes, and test your changes on a staging site before implementing them on a live site.

Posted in Uncategorized