WordPress the_title_attribute() Function

Beginner
6 min read
Updated: Nov 2026
3 examples
Prints an escaped title

Definition

In WordPress, the_title_attribute() prints the post title in a safe form for attributes. Use it in title="", alt="", or aria-label="".

It removes HTML tags and escapes quotes, so the page HTML cannot break.

Syntax

A few ways to call the_title_attribute().

WordPress
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
CallWhat it does
the_title_attribute()Print the escaped title.
the_title_attribute( array( 'before' => 'Read ' ) )Add text before the title.
the_title_attribute( array( 'echo' => false ) )Return the title instead of printing.
the_title_attribute( array( 'post' => 12 ) )Use the title of the post with ID 12.

It takes one array of options. The options are before, after, echo, and post.

Full declaration
WordPress
the_title_attribute( string|array $args = '' ): string|void

Parameters

ParameterDescription
beforestring. Text before the title. Default is empty.
afterstring. Text after the title. Default is empty.
echobool. true prints the title. false returns it. Default is true.
postint|WP_Post. The post to use. Default is the current post.

Return Value

Return ValueDescription
voidWhen echo is true, the title is printed.
stringWhen echo is false, the title is returned.

WordPress Examples

Three short examples. Each one shows the code and what it prints.

Example 1 — Title attribute on a link

Use it for the title of a post link.

WordPress
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
  Read more
</a>

How It Works

  1. the_title_attribute() prints the title with quotes escaped.
  2. It sits inside the title attribute.
  3. The link text is ours.

Example 2 — Add text before

Use before for a clear label.

WordPress
<a href="<?php the_permalink(); ?>"
   aria-label="<?php the_title_attribute( array( 'before' => 'Read more about ' ) ); ?>">
  Read more
</a>

How It Works

  1. The options go in one array.
  2. before adds text in front of the title.
  3. The aria-label helps screen reader users.

Example 3 — Return the title

Set echo to false to store the text.

WordPress
<?php
$label = the_title_attribute( array( 'echo' => false ) );
echo '<img src="cover.jpg" alt="' . $label . '">';

How It Works

  1. echo set to false returns the title.
  2. It is already escaped for attributes.
  3. We place it in the alt attribute.

Frequently Asked Questions

It prints the post title in an escaped form for use inside an HTML attribute.
the_title() is for page text. the_title_attribute() is for attributes, and it removes tags and escapes quotes.
before, after, echo, and post, all inside one array.
Pass array( 'echo' => false ).
Not if you pass a post in the options.

Browse all post functions

Open the functions index for simple guides with examples and FAQs.

Post Functions Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful