Using WordPress ‘the_title()’ PHP function

The the_title() WordPress PHP function displays or retrieves the current post title with optional markup.

Usage

the_title( $before, $after, $display );

Parameters

  • $before (string) Optional: Markup to prepend to the title. Default: ''
  • $after (string) Optional: Markup to append to the title. Default: ''
  • $display (bool) Optional: Whether to echo or return the title. Default true for echo. Default: true

More information

See WordPress Developer Resources: the_title()

Examples

Display the title with custom HTML tags

Display the post title with an <h1> tag before and a </h1> tag after it.

the_title( '<h1>', '</h1>' );

Wrap the title in a custom CSS class

Wrap the post title with a <span> tag having the CSS class my-custom-title.

the_title( '<span class="my-custom-title">', '</span>' );

Retrieve the title and store it in a variable

Get the post title without displaying it and store it in a variable $post_title.

$post_title = the_title( '', '', false );

Display title with a custom separator

Display the post title with a custom separator (e.g. |) after it.

the_title( '', ' |' );

Display title with custom text before and after

Display the post title with custom text (e.g. "Read: " before and " - A Great Post" after it).

the_title( 'Read: ', ' - A Great Post' );