single_term_title()WP 3.1.0

Display or retrieve title for the current taxonomy (category, tag, etc.). Uses on term archive pages.

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.

1 time — 0.000001 sec (speed of light) | 50000 times — 0.02 sec (speed of light) | PHP 7.1.11, WP 4.9.7

Return

String|null. The title of the current term (category, tag, etc.). null will be returned if no header was obtained or when the result is displayed.

Usage

<?php single_term_title( $prefix, $display ); ?>
$prefix(string)
What to display before the title.
Default: ''
$display(true/false)
Whether to display or retrieve title.
Default: true

Examples

0

#1 Display current taxonomy title

Let's display the title of the current taxonomy term "Textbooks for students" with the preceding text "You are viewing the section: ". It can be term of build-in taxonomy or any custom taxonomy:

<h1><?php single_term_title('You are viewing the section: '); ?>.</h1>

Displays: <h1>You are viewing the section: Textbooks for students</h1>

0

#2 Get the term title as variable

This example shows how to obtain a term name for the variable $term_title, but not display it on the screen:

<?php $term_title = single_term_title('', 0); ?>

Changelog

Since 3.1.0 Introduced.

single_term_title() code WP 6.4.3

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;
	}
}