the_title_attribute() WP 2.3.0
Displays the title of the post prepared (sanitized) for use in the HTML attributes of HTML tag.
The function should be used inside a WordPress Loop.
Works like the_title(), except the parameters can be in a string or an array. See the function for what can be overridden in the $args parameter.
The function is identical to the_title(), the only difference is that this function "cleans" the title from HTML tags and sanitize HTML entities (<, >, ", '). For example, symbol <
will be replaced by <
. Before the output it pass the post title through functions: esc_attr() and strip_tags().
No Hooks.
Return
null/String. Void if 'echo' argument is true, the title attribute if 'echo' is false.
Usage
the_title_attribute( $args );
- $args(string/array)
Title attribute arguments. Optional.
-
before(string)
Markup to prepend to the title.
Default: '' -
after(string)
Markup to append to the title.
Default: '' -
echo(true/false)
Whether to echo or return the title.
Default: true for echo - post(WP_Post)
Current post object to retrieve the title for.
Default: ''
-
Examples
#1 An example of using the function in the title
attribute of the <a>
tag.
Since this attribute does not allow to use HTML tags, quotes, and other things, we can't use the_title() function, but the_title_attribute() suit perfectly here:
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute([ 'before'=>'Permalink to: ' ]); ?>"> <?php the_title(); ?> </a>
Changelog
Since 2.3.0 | Introduced. |
Code of the_title_attribute() the title attribute WP 5.6
Related Functions
From tag: title (of post page)
More from category: Posts, pages...
- edit_post_link()
- get_delete_post_link()
- get_edit_post_link()
- get_permalink()
- get_post_field()
- get_post_status()
- get_post_time()
- get_sample_permalink()
- get_the_content()
- get_the_date()
More from Template Tags: Posts, Pages, ...
- body_class()
- get_post_format()
- have_posts()
- in_the_loop()
- next_image_link()
- next_post_link()
- next_posts_link()
- post_class()