Example 1 — Print the post title
Show the title in the page header.
<h1><?php single_post_title(); ?></h1> How It Works
- The visitor is on a single post.
- The function prints the post title.
- No loop is needed.

In WordPress, single_post_title() shows the title of the current post or Page. It works outside the loop, which makes it useful in headers and the page title.
It also works on the blog posts page when you use a static front page, where it prints the title of that Posts page.
Call it in a header or any template file.
<?php
single_post_title(); | Call | What it does |
|---|---|
single_post_title() | Prints the current post or Page title. |
single_post_title( 'You are reading: ' ) | Prints the title with a prefix. |
single_post_title( '', false ) | Returns the title instead of printing. |
Inside the loop, the_title() is the usual choice.
single_post_title( string $prefix = '', bool $display = true ): string|void | Parameter | Description |
|---|---|
$prefix | string. Text to print before the title. Default is empty. |
$display | bool. true (default) prints the title. false returns it. |
| Return Value | Description |
|---|---|
| void | When $display is true, the title is printed. |
| string | When $display is false, the title is returned (without the prefix). |
Three short theme examples. Each one shows the code and what it prints.
Show the title in the page header.
<h1><?php single_post_title(); ?></h1> Print "Now reading: Hello World".
<p><?php single_post_title( 'Now reading: ' ); ?></p> Use the title in an attribute.
<?php
$title = single_post_title( '', false );
echo '<meta name="x-post-title" content="' . esc_attr( $title ) . '">'; false returns the title as a string.esc_attr() makes it safe for an attribute.Print or return the name of the current tag on a tag archive.
8 people found this page helpful