問題描述
我的主題是支持 title-tag add_theme_support('title-tag'),但是我不能在主頁上從<title> 中刪除 wordpress 描述 bloginfo('description')。
我正在嘗試使用此過濾器沒有成功:
add_filter( 'wp_title', function ( $title, $sep ) {
global $paged, $page;
$title .= get_bloginfo( 'name' );
if ( is_home() || is_front_page() )
$title = "$title";
return $title;
}, 10, 2 );
最佳解決方案
wp_get_document_title()有一些有趣的過濾器 – pre_get_document_title 和 document_title_parts 。
/**
* Filter the parts of the document title.
*
* @since 4.4.0
*
* @param array $title {
* The document title parts.
*
* @type string $title Title of the viewed page.
* @type string $page Optional. Page number if paginated.
* @type string $tagline Optional. Site description when on home page.
* @type string $site Optional. Site title when not on home page.
* }
*/
add_filter( 'document_title_parts', function ( $title ) {
if ( is_home() || is_front_page() )
unset($title['tagline']);
return $title;
}, 10, 1 );
回顧一下 pre_get_document_title 濾鏡非常有趣。在處理標題之前,它將運行此過濾器。如果結果不為空 (這不是預期的),則該過程是 short-circuited 。
$title = apply_filters( 'pre_get_document_title', '' );
if ( ! empty( $title ) ) {
return $title;
}
這意味着如果您定義了標題,則不必擔心任何其他事項。好的是,你可以對規則做出例外。所以回答你原來的問題:
add_filter( 'pre_get_document_title', function( $title ) {
if ( is_home() || is_front_page() ) {
// Return blog title on front page
$title = get_bloginfo( 'name' );
}
return $title;
} );
次佳解決方案
找到一個解決方案:
add_filter( 'pre_get_document_title', function ( $title ) {
if(is_front_page()){
$title = get_bloginfo();
}
return $title;
});
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。