the_title
Applies to the post title, before retrieving or displaying it.
Post title normally displays with the_title() or retrieves with get_the_title() functions. Those functions trigger this filter.
In some cases, such as when the_title() is used, the title can be removed by returning an empty (NULL, FALSE or '') value in this filter.
Usage
add_filter( 'the_title', 'wp_kama_the_title_filter', 10, 2 ); /** * Function for `the_title` filter-hook. * * @param string $post_title The post title. * @param int $post_id The post ID. * * @return string */ function wp_kama_the_title_filter( $post_title, $post_id ){ // filter... return $post_title; }
- $title(string)
- Post title.
- $post_id(int)
- Post ID.
Examples
#1 Let's add the word "Page" to the title of the static pages:
A demonstration of how the filter works:
add_filter( 'the_title', 'add_text_to_page_title' ); function add_text_to_page_title( $title ) { if( is_page() ){ $title = 'Page: '. $title; } return $title; }
As a result, the title of all static pages will look like this: Page: Page Title
.
#2 Deleting titles in a category
This example shows how to remove headers from entries that are in the "advertise" category:
add_filter( 'the_title', 'suppress_if_blurb', 10, 2 ); function suppress_if_blurb( $title, $id = null ) { if ( in_category( 'advertise', $id ) ) { return ''; } return $title; }
The optional parameter $id = null, the default value for the second parameter, is specified because some function calls may not pass this parameter. This bug appeared in version 3.1 and was fixed in version 3.3. If backward compatibility with versions 3.1-3.3 is needed then you must specify the default value for $id, otherwise you will get a PHP warning that no required parameter is specified.
Changelog
Since 0.71 | Introduced. |
Where the hook is called
return apply_filters( 'the_title', $post_title, $post_id );
$string .= apply_filters('the_title', $post->post_title, $post->ID);
$string .= apply_filters('the_title', $post->post_title, $post->ID);
$title = apply_filters('the_title', $title, $post->ID);
$title = apply_filters('the_title', $title, $post->ID);
$post_title = apply_filters( 'the_title', $post->post_title, $post->ID );
$text = strip_tags( apply_filters( 'the_title', $result->post_title, $result->ID ) );
$title = apply_filters( 'the_title', $title, $post->ID );
$original_title = apply_filters( 'the_title', $original_object->post_title, $original_object->ID );
$title = apply_filters( 'the_title', $menu_item->title, $menu_item->ID );
$original_title = apply_filters( 'the_title', $original_object->post_title, $original_object->ID );
apply_filters( 'the_title', $page->post_title, $page->ID ),
$title = apply_filters( 'the_title', $menu_item->title, $menu_item->ID );
$data['title']['rendered'] = apply_filters( 'the_title', $template->title, $template->wp_id );
$title = apply_filters( 'the_title', $menu_item->post_title, $menu_item->ID );
$parent_name = apply_filters( 'the_title', $parent->post_title, $parent->ID );
Where the hook is used in WordPress
add_filter( 'the_title', 'esc_html' );
add_filter( 'the_title', 'esc_html' );
add_filter( $filter, 'capital_P_dangit', 11 );
add_filter( 'the_title', 'wptexturize' );
add_filter( 'the_title', 'convert_chars' );
add_filter( 'the_title', 'trim' );