Remove “Category:”, “Tag:”, “Author:” from the_archive_title
Frage
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.
Lösung
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;
});
Andere Tipps
Use function single_term_title()
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();
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>
echo '<h1 class="page-title">' . single_cat_title( '', false ) . '</h1>';
in taxonomy-category.php outside public of theme.
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;
}
as of Wordpress 5.5 you can remove the prefix with this hook:
add_filter('get_the_archive_title_prefix','__return_false');
You can use the following to just have only the title without the prefix
single_cat_title();
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);
?>
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