WordPress как на ладони
rgbcode is looking for WordPress developers.

the_titleхук-фильтрWP 0.71

Применяется к заголовку записи, перед тем как получить или вывести заголовок на экран функциями: the_title() или get_the_title().

В некоторых случаях, например когда используется функция the_title(), заголовок можно удалить, вернув пустое значение в фильтре (NULL, FALSE или '').

Использование

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(строка)
Заголовок записи.
$post_id(число)
ID записи.

Примеры

1

#1 Добавим слово "Страница" к заголовку постоянных страниц:

Демонстрация работы фильтра:

add_filter( 'the_title', 'add_text_to_page_title' );
function add_text_to_page_title( $title ) {
	if( is_page() )
		$title = 'Страница: '. $title;

	return $title;
}

В результате заголовок всех постоянных страниц будет выглядеть так: Страница: Заголовок страницы.

0

#2 Удаление заголовков в категории

В этом примере показано, как удалить заголовки у записей, которые находятся в категории "reklama":

add_filter( 'the_title', 'suppress_if_blurb', 10, 2 );
function suppress_if_blurb( $title, $id = null ) {

	if ( in_category('reklama', $id ) ){
		return '';
	}

	return $title;
}

Дополнительный параметр $id = null, значение по умолчанию для второго параметра, указан потому что, некоторые вызовы функции могут не передавать этот параметр. Эта ошибка появилась в версии 3.1 и была исправлена в версии 3.3. Если нужна обратная совместимость с версиями 3.1-3.3, то нужно указать значение по умолчанию для $id, иначе вы получите PHP предупреждение, что не указан обязательный параметр.

Список изменений

С версии 0.71 Введена.

Где вызывается хук

get_the_title()
the_title
Walker_Page::start_el()
the_title
wp_setup_nav_menu_item()
the_title
previous_post()
the_title
next_post()
the_title
get_boundary_post_rel_link()
the_title
get_parent_post_rel_link()
the_title
WP_Customize_Nav_Menu_Item_Setting::get_original_title()
the_title
WP_REST_Templates_Controller::prepare_item_for_response()
the_title
WP_REST_Menu_Items_Controller::prepare_item_for_response()
the_title
do_trackbacks()
the_title
get_adjacent_post_link()
the_title
Walker_Nav_Menu::start_el()
the_title
wp_get_archives()
the_title
WP_Posts_List_Table::column_title()
the_title
Walker_Nav_Menu_Checklist::start_el()
the_title
wp-includes/post-template.php 174
return apply_filters( 'the_title', $post_title, $post_id );
wp-includes/class-walker-page.php 206
apply_filters( 'the_title', $page->post_title, $page->ID ),
wp-includes/nav-menu.php 880
$original_title = apply_filters( 'the_title', $original_object->post_title, $original_object->ID );
wp-includes/deprecated.php 151
$string .= apply_filters('the_title', $post->post_title, $post->ID);
wp-includes/deprecated.php 186
$string .= apply_filters('the_title', $post->post_title, $post->ID);
wp-includes/deprecated.php 2697
$title = apply_filters('the_title', $title, $post->ID);
wp-includes/deprecated.php 2772
$title = apply_filters('the_title', $title, $post->ID);
wp-includes/customize/class-wp-customize-nav-menu-item-setting.php 273
$original_title = apply_filters( 'the_title', $original_object->post_title, $original_object->ID );
wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php 695
$data['title']['rendered'] = apply_filters( 'the_title', $template->title, $template->wp_id );
wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php 515
$title = apply_filters( 'the_title', $menu_item->title, $menu_item->ID );
wp-includes/comment.php 2993
$post_title = apply_filters( 'the_title', $post->post_title, $post->ID );
wp-includes/link-template.php 2337
$title = apply_filters( 'the_title', $title, $post->ID );
wp-includes/class-walker-nav-menu.php 259
$title = apply_filters( 'the_title', $menu_item->title, $menu_item->ID );
wp-includes/general-template.php 2186
$text = strip_tags( apply_filters( 'the_title', $result->post_title, $result->ID ) );
wp-admin/includes/class-wp-posts-list-table.php 1106
$parent_name = apply_filters( 'the_title', $parent->post_title, $parent->ID );
wp-admin/includes/class-walker-nav-menu-checklist.php 102
$title = apply_filters( 'the_title', $menu_item->post_title, $menu_item->ID );

Где используется хук в WordPress

wp-admin/includes/class-wp-media-list-table.php 722
add_filter( 'the_title', 'esc_html' );
wp-admin/includes/class-wp-posts-list-table.php 805
add_filter( 'the_title', 'esc_html' );
wp-includes/default-filters.php 162
add_filter( $filter, 'capital_P_dangit', 11 );
wp-includes/default-filters.php 188
add_filter( 'the_title', 'wptexturize' );
wp-includes/default-filters.php 189
add_filter( 'the_title', 'convert_chars' );
wp-includes/default-filters.php 190
add_filter( 'the_title', 'trim' );
3 комментария
    Войти