Example 1 — Print the page title
Use it in a heading on a single post.
<h1><?php single_post_title(); ?></h1> How It Works
- The function prints the title of the opened post.
- No Loop is needed.
- It is the same title shown in the editor.

In WordPress, single_post_title() shows the title of the current single post or page. It is often used in header.php to build the page title.
It reads the main query, so you do not need The Loop.
A few ways to call single_post_title().
<title><?php single_post_title( '', true ); ?></title> | Call | What it does |
|---|---|
single_post_title() | Print the title. |
single_post_title( 'Reading: ' ) | Print the title with text in front. |
single_post_title( '', false ) | Return the title instead of printing. |
It uses the main query, so it shows the title of the post or page the visitor opened, even outside The Loop.
single_post_title( string $prefix = '', bool $display = true ): string|void | Parameter | Description |
|---|---|
$prefix | string. Text placed before the title. Default is empty. |
$display | bool. true prints the result. false returns it. Default is true. |
| Return Value | Description |
|---|---|
| void | When $display is true, the title is printed. |
| string | When $display is false, the title is returned. |
Three short examples. Each one shows the code and what it prints.
Use it in a heading on a single post.
<h1><?php single_post_title(); ?></h1> Put text in front of the title.
<h1><?php single_post_title( 'Reading: ' ); ?></h1> Set $display to false to get a string.
<?php
$title = single_post_title( '', false );
echo '<meta property="og:title" content="' . esc_attr( $title ) . '">'; false returns the title.esc_attr() is used because the title goes into an attribute.Open the functions index for simple guides with examples and FAQs.
13 people found this page helpful