74

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.

10 Answers 10

117

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;    
    });
7
  • 2
    That works! Thanks, Pieter! In case less tech-savy users are looking for the same solution: You add Pieter's code to your functions.php file and that's it!
    – Nick
    Feb 27 '15 at 21:05
  • Do this in a child theme so your changes won't get lost upon a theme update. Aug 19 '16 at 7:01
  • 1
    This works pretty well, but it's missing cases for archives and custom taxonomies. Nov 23 '16 at 16:57
  • See Ben Gillbanks' solution to all post type archives and taxonomies.
    – Dan Knauss
    Jan 27 '18 at 5:54
  • Just added support for CPT archives :) Mar 9 '20 at 20:43
39

Use function single_term_title()

4
  • 1
    You can do things many ways in WP, but I always opted for the simplest solutions. Thanks Timo.
    – f055
    Jan 11 '18 at 14:22
  • 2
    Well that just doesn't work for all archives that aren't terms. So in my opinion not a very good solution.
    – GDY
    Apr 5 '19 at 7:13
  • @GDY could you tell, which non-terms should show in archives?
    – Iggy
    Dec 17 '19 at 5:19
  • this returns 'null' to me, is there any setting that I should set? Dec 15 '20 at 12:38
19

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();

From developer.wordpress.org

1
  • Nice - this function is just what I needed. Note that the first parameter already defaults to and empty string and the second defaults to true which would echo out the result instead of returning it... so you could just use post_type_archive_title() to get the exact same result as echo post_type_archive_title( '', false ) Dec 8 '19 at 23:32
10

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>
1
  • 5
    this wouldn't work for an non-english website. Nov 9 '18 at 15:50
3

echo '<h1 class="page-title">' . single_cat_title( '', false ) . '</h1>'; in taxonomy-category.php outside public of theme.

3

as of Wordpress 5.5 you can remove the prefix with this hook:

add_filter('get_the_archive_title_prefix','__return_false');

2

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; 
}
1
  • This is a nice solution, but really you should check is_category() before updating $title to single_cat_title, & if not is_category(), then just return the $title unchanged..
    – Paul 501
    Aug 20 '20 at 22:53
1

You can use the following to just have only the title without the prefix

single_cat_title();
1
  • 2
    While true, this has been suggested before in other answers.
    – Nicolai
    Feb 27 '18 at 18:52
0

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);
?>
-6

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
4
  • 3
    Err, are you really suggesting hacking the core here?
    – cjbj
    Sep 6 '16 at 13:42
  • 4
    This is a very, very bad idea. You cannot update now without creating a separate branch.
    – fuxia
    Sep 6 '16 at 13:57
  • Sory if I break roles I give a word for that, but this is just another solution nothing malicious. So, you said that I hack my own site or I cant share code from core files? Sep 6 '16 at 17:36
  • Well, it's your website and you may edit whatever you want - even the core files. However, the web will break if you update your Wordpress...
    – Raccoon
    Sep 22 '16 at 0:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.