Displays or retrieves page title for taxonomy term archive.
Description
Useful for taxonomy term template files for displaying the taxonomy term page title.
The prefix does not automatically place a space between the prefix, so if there should be a space, the parameter value will need to have it at the end.
Parameters
$prefix
stringoptional- What to display before the title.
Default:
''
$display
booloptional- Whether to display or retrieve title.
Default:
true
Source
function single_term_title( $prefix = '', $display = true ) {
$term = get_queried_object();
if ( ! $term ) {
return;
}
if ( is_category() ) {
/**
* Filters the category archive page title.
*
* @since 2.0.10
*
* @param string $term_name Category name for archive being displayed.
*/
$term_name = apply_filters( 'single_cat_title', $term->name );
} elseif ( is_tag() ) {
/**
* Filters the tag archive page title.
*
* @since 2.3.0
*
* @param string $term_name Tag name for archive being displayed.
*/
$term_name = apply_filters( 'single_tag_title', $term->name );
} elseif ( is_tax() ) {
/**
* Filters the custom taxonomy archive page title.
*
* @since 3.1.0
*
* @param string $term_name Term name for archive being displayed.
*/
$term_name = apply_filters( 'single_term_title', $term->name );
} else {
return;
}
if ( empty( $term_name ) ) {
return;
}
if ( $display ) {
echo $prefix . $term_name;
} else {
return $prefix . $term_name;
}
}
Hooks
- apply_filters( ‘single_cat_title’,
string $term_name ) Filters the category archive page title.
- apply_filters( ‘single_tag_title’,
string $term_name ) Filters the tag archive page title.
- apply_filters( ‘single_term_title’,
string $term_name ) Filters the custom taxonomy archive page title.
Changelog
Version | Description |
---|---|
3.1.0 | Introduced. |
This example displays the text “Currently browsing ” followed by the term title.
Output:
Currently browsing WordPress.
This example assigns the current term title to the variable
$current_term
for use in PHP.