WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/the title attribute

Description

Displays or returns the title of the current post. It somewhat duplicates the functionality of the_title(), but provides a “clean” version of the title for use in HTML attributes by stripping HTML tags with strip_tags() and by converting certain characters (including quotes) to their character entity equivalent with esc_attr(); it also uses query-string style parameters. This tag must be within The Loop.

Usage

 <?php the_title_attribute$args ); ?> 

Parameter $arg

  • $args (string|array)
$before
(string) (optional) Text to place before the title.
Default: None
$after
(string) (optional) Text to place after the title.
Default: None
$echo
(Boolean) (optional) Display the title (TRUE) or return it for use in PHP (FALSE).
Default: TRUE
$post
(integer or object) (optional) The ID of the post or an object that specifies the post from which to get the title. By default the current post is fetched.
Default: null

Example

Inline with array args

<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute( array( 'before' => 'Permalink to: ', 'after' => '' ) ); ?>"><?php the_title(); ?></a>

Inline with text args

<?php
function child_do_post_title() {
?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute( 'before=Permalink to: "&after="' ); ?>"><?php the_title(); ?></a>
<?php
}
?>

PHP with text args

printf(	'<a href="%s" title="%s">%s</a>', get_permalink(), the_title_attribute( 'echo=0' ), get_the_title() );

Notes

Change Log

  • Since: 2.3.0

Source File

the_title_attribute() is located in wp-includes/post-template.php.

Related

See also index of Function Reference and index of Template Tags.