To remove the “Category” prefix from category archives in WordPress, you can use the get_the_archive_title
filter hook along with a custom function. This code snippet will remove the prefix and display only the category name.
function wpsnippets_remove_category_prefix( $title ) {
if ( is_category() ) {
$title = single_cat_title( '', false );
}
return $title;
}
add_filter( 'get_the_archive_title', 'wpsnippets_remove_category_prefix' );
This code snippet checks if the current page is a category archive using the is_category()
function. If it is, it retrieves the category title without the prefix using the single_cat_title()
function and assigns it to the $title
variable. Finally, the modified title is returned.
You can add this code to your theme’s functions.php
file or create a custom plugin for it. Once added, the “Category” prefix will be removed from category archive pages.
Examples
Example 1: Remove “Category” prefix from category archives using a filter
This code example demonstrates how to remove the “Category” prefix from category archives by modifying the archive title using the get_the_archive_title
filter.
function wpsnippets_remove_category_prefix( $title ) {
if ( is_category() ) {
$title = single_cat_title( '', false );
}
return $title;
}
add_filter( 'get_the_archive_title', 'wpsnippets_remove_category_prefix' );
In this code, we define a custom function wpsnippets_remove_category_prefix
that checks if the current page is a category archive using the is_category()
conditional tag. If it is, we modify the archive title by calling single_cat_title()
with an empty prefix and passing false
as the second parameter to exclude the “Category” prefix. Finally, we hook this function to the get_the_archive_title
filter using add_filter()
.
Example 2: Remove “Category” prefix from category archives using a template override
This code example demonstrates how to remove the “Category” prefix from category archives by creating a custom template override.
function wpsnippets_custom_category_title() {
if ( is_category() ) {
$title = single_cat_title( '', false );
echo esc_html( $title );
}
}
In this code, we define a custom function wpsnippets_custom_category_title
that checks if the current page is a category archive using the is_category()
conditional tag. If it is, we retrieve the category title without the “Category” prefix by calling single_cat_title()
with an empty prefix and passing false
as the second parameter. Finally, we output the modified title using echo
and escape it with esc_html()
to ensure proper sanitization.
To use this custom template override, create a new file named category.php
in your theme’s directory and replace the default category archive template with the code above.
Example 3: Remove “Category” prefix from category archives using a plugin
This code example demonstrates how to remove the “Category” prefix from category archives by creating a custom plugin.
/**
* Plugin Name: Remove Category Prefix
* Description: Removes the "Category" prefix from category archives.
*/
function wpsnippets_remove_category_prefix() {
if ( is_category() ) {
$title = single_cat_title( '', false );
return esc_html( $title );
}
return false;
}
In this code, we define a custom function wpsnippets_remove_category_prefix
that checks if the current page is a category archive using the is_category()
conditional tag. If it is, we retrieve the category title without the “Category” prefix by calling single_cat_title()
with an empty prefix and passing false
as the second parameter. We then return the modified title after sanitizing it with esc_html()
. Finally, we wrap the entire function in a plugin header comment to create a valid WordPress plugin.
To use this plugin, create a new directory in the wp-content/plugins/
directory of your WordPress installation and name it something like remove-category-prefix
. Then, create a new PHP file inside that directory and name it remove-category-prefix.php
. Paste the code above into this file and activate the plugin from the WordPress admin dashboard.