the_archive_title()
Display the archive page title, based on the type of page (tag, term, category, date).
Used for returning/displaying the title of the current term
, date
, post type
, post format
, or author
archive.
-
Use get_the_archive_title(), to get the title for processing in a variable.
- Use the_archive_description(), to display description of category, tag, term, or author.
Uses: get_the_archive_title()
No Hooks.
Return
null
. Nothing (null).
Usage
the_archive_title( $before, $after );
- $before(string)
- Content to prepend to the title.
Default: '' - $after(string)
- Content to append to the title.
Default: ''
Examples
#1 Print the header for the archive pages
Suppose we have a template file index.php
, handles all the archive pages and we need to specify each of them a different header, for example:
- If a category, then:
"Category: optimization"
. - If tag, then:
"Tag: coding"
. - If archive by year, then:
"Year: 2104"
.
Before WP 4.1 we did this with a combination of conditions if ... else ... and conditional tags, but now we can use just one line:
<h1><?php the_archive_title() ?></h1>
#2 Remove the "Category:", "Tag:" prefix on the archives page
Since WP 5.5 we can do it using get_the_archive_title_prefix hook:
// remove “Category:”, “Tag:”, “Author:” ... archive title add_filter( 'get_the_archive_title_prefix', '__return_empty_string' );
Prior WP 5.5, use the following hack:
// Removes "Category: ", "Tag: " etc. from the archive header add_filter( 'get_the_archive_title', function( $title ){ return preg_replace( '~^[^:]+: ~', '', $title ); } );
#3 More examples
See here: get_the_archive_title().
Notes
Changelog
Since 4.1.0 | Introduced. |
the_archive_title() the archive title code WP 6.7.1
function the_archive_title( $before = '', $after = '' ) { $title = get_the_archive_title(); if ( ! empty( $title ) ) { echo $before . $title . $after; } }