Using WordPress ‘get_the_archive_title()’ PHP function

The get_the_archive_title() WordPress PHP function retrieves the archive title based on the queried object.

Usage

echo get_the_archive_title();

Parameters

  • None

More information

See WordPress Developer Resources: get_the_archive_title()

Examples

Remove archive label

Remove the “Category:”, “Tag:”, “Author:”, “Archives:”, and “Other taxonomy name:” in the archive title.

function my_theme_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>';
    } elseif ( is_post_type_archive() ) {
        $title = post_type_archive_title( '', false );
    } elseif ( is_tax() ) {
        $title = single_term_title( '', false );
    }
    return $title;
}
add_filter( 'get_the_archive_title', 'my_theme_archive_title' );

Remove “Archive:” prefix

Remove the “Archive:” prefix from the archive title.

add_filter( 'get_the_archive_title_prefix', '__return_empty_string' );

Add text or execute a function before the archive title

Modify the archive title by adding text or executing a function before the title.

function modify_archive_title( $title ) {
    $var = "1";
    return $var . $title;
}
add_filter( 'get_the_archive_title', 'modify_archive_title', 10, 1 );

Display custom post type archive title without “Archive:”

Output just the title of the custom post type with no extra word like “Archive”.

echo post_type_archive_title( '', false );

Get category or custom post type archive title

Retrieve the category title or custom post type archive title.

$q = get_queried_object();
$title = is_category() ? $q->name : $q->labels->name;