WordPress single_term_title() Function

Definition
In WordPress, single_term_title() gives you the name of the term on a term archive page. It works for categories, tags, and custom taxonomies.
By default it prints the name. Set $display to false to return the name instead. Use it when one template serves many taxonomies.
Syntax
Call it on any category, tag, or taxonomy archive. Pass a prefix, or set display to false.
single_term_title();
single_term_title( 'Archive: ' );
$name = single_term_title( '', false );
| Call | What it does |
|---|
single_term_title() | Prints the name of the current term. |
single_term_title( 'Archive: ' ) | Prints the prefix, then the name. |
single_term_title( '', false ) | Returns the name as a string. Nothing is printed. |
Use it in archive.php or taxonomy.php. It does nothing on other pages.
Full declaration
single_term_title( string $prefix = '', bool $display = true ): string|void
Parameters
| Parameter | Description |
|---|
$prefix | Optional. Text placed before the name when it is printed. Default is an empty string. |
$display | Optional. true prints the name (default). false returns it. |
Return Value
| Return Value | Description |
|---|
| string | The term name. Returned only when $display is false. |
| void | Nothing is returned when the name is printed, or when the page is not a term archive. |
WordPress Examples
Three short theme examples. Each one has a sample output.
Example 1 — Print the title
Show the term name in a heading on any term archive.
<h1><?php single_term_title(); ?></h1>
How It Works
- On a term archive the function prints the term name.
- The same code works for categories, tags, and custom taxonomies.
- No
echo is needed because it prints by itself.
Example 2 — Add a prefix
Print a label before the term name.
<h1><?php single_term_title( 'Archive: ' ); ?></h1>
How It Works
- The first argument is the prefix.
- WordPress prints the prefix and then the term name.
- Include the space inside the prefix text.
Example 3 — Return the name
Get the name as a string and use it in a page title.
<?php
$name = single_term_title( '', false );
if ( $name ) {
echo '<h1>Posts about ' . esc_html( $name ) . '</h1>';
}
?>
<h1>Posts about Drama</h1>
How It Works
false tells the function to return the name.if ( $name ) checks that this is a term archive.esc_html() keeps the output safe.
Frequently Asked Questions
It prints the name of the current category, tag, or custom taxonomy term on an archive page. With $display set to false, it returns the name.
Use it when one template, such as archive.php, shows many kinds of archives. It works for categories, tags, and custom taxonomies.
No. It only works on term archive pages.
Pass false as the second argument: single_term_title( '', false ).
No. The prefix is added only when the name is printed.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful