I have the following code in my theme’s archive.php:
<?php the_archive_title( '<h1 class="page-title">', '</h1>' ); ?>
This gives me titles like “Category: Russia”, “Tag: America”, “Author: John”.
I would like to remove the “Category:”, “Tag:” and “Author:” part and just display the category, tag and author names.
Does anyone know how to accomplish this?
Thank you.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can extend the get_the_archive_title filter which I’ve mentioned in this answer
add_filter('get_the_archive_title', function ($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_tax()) { //for custom post types
$title = sprintf(__('%1$s'), single_term_title('', false));
} elseif (is_post_type_archive()) {
$title = post_type_archive_title('', false);
}
return $title;
});
Method 2
Use function single_term_title()
Method 3
For CPT title Without word: ‘Archive’:
If you are building custom archive template for a CPT, and want to output just the title of the CPT with no extra word like “Archive” use following function instead:
post_type_archive_title();
Method 4
I feel like this is over simplifying things, but this is what I did…
<h1><?php echo str_replace("Archives: ", "", get_the_archive_title()); ?></h1>
Method 5
as of WordPress 5.5 you can remove the prefix with this hook:
add_filter('get_the_archive_title_prefix','__return_false');
Method 6
echo '<h1 class="page-title">' . single_cat_title( '', false ) . '</h1>';
in taxonomy-category.php outside public of theme.
Method 7
I would use a filter and put it in a file functions.php
add_filter( 'get_the_archive_title', 'replaceCategoryName');
function replaceCategoryName ($title) {
$title = single_cat_title( '', false );
return $title;
}
Method 8
You can use the following to just have only the title without the prefix
single_cat_title();
Method 9
Assuming the format is always: Prefix: Archive Name, we can just find the first colon followed by a space, and only show the content after this, using get_the_archive_title() with PHP’s substr() and strpos() functions.
<?php // Only show the portion of the string following the first ": " echo substr(get_the_archive_title(), strpos(get_the_archive_title(), ': ') + 2); ?>
Method 10
add_filter('post_type_archive_title', '__return_empty_string', 5);
Method 11
directory: wp-includes
file: general-template.php
find function: get_the_archive_title()
change:
if ( is_category() ) {
$title = sprintf( __( 'Category: %s' ), single_cat_title( '', false ) );
} elseif ( is_tag() ) {
$title = sprintf( __( 'Tag: %s' ), single_tag_title( '', false ) );
} elseif ( is_author() ) {
$title = sprintf( __( 'Autor: %s' ), '<span class="vcard">' . get_the_author() . '</span>' );
}
to:
if ( is_category() ) {
$title = sprintf( __( '%s' ), single_cat_title( '', false ) );
} elseif ( is_tag() ) {
$title = sprintf( __( '%s' ), single_tag_title( '', false ) );
} elseif ( is_author() ) {
$title = sprintf( __( '%s' ), '<span class="vcard">' . get_the_author() . '</span>' );
}//if you want to remove or just change text if you need to
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0