問題描述
WordPress version 4.5.1
我正在嘗試動態更新特定模板上的頁面標題。經過大量的挖掘和了解 wp_title()的變化,我試圖使用 document_title_parts 。但是,我無法讓過濾器執行。
我在一個小孩主題,functions.php:
add_theme_support( 'title-tag' );
//add_filter("after_setup_theme", function(){ add_theme_support("title-tag"); });
add_filter( 'document_title_parts', function( $title )
{
error_log('here');
return $title;
}, 10, 1 );
我已經嘗試了新增主題支援的兩個變體,如上所示,但是看著我的日誌,沒有頁面重新載入。 error_log 正在使用其他功能 (如 wp_title),因此錯誤記錄工作正常。
我也嘗試過 pre_get_document_title,它在頁面載入時觸發,儘管我無法讓它實際更改標題。
所以!我使用過濾器錯誤,沒有正確設定我的主題,或者我不知道的其他東西。任何幫助將不勝感激!
編輯以新增更多細節
嘗試一個 init 函式,但是也不行:https://gist.github.com/anonymous/6db5af892a4cf4fb029655167d7002a4
此外,雖然我從 header.php 中刪除了對<title> 的任何引用,但實際的網站標題仍然顯示在原始檔中。
最佳解決方案
我在我的開發區域執行過濾器。它沒有工作。然後我關閉了 Yoast SEO 外掛,我知道這也是搞亂頁面標題的。然後它工作。所以我的建議是另外一個外掛搞砸了。
在 Yoast 的情況下,它是一個過濾器呼叫 pre_get_document_title 返回非空。在這種情況下,wp_get_document_title 短路,其餘功能 (包括 documents_title_parts 濾波器) 未進行評估,正如您可以從第一行程式碼中看到的:
$title = apply_filters( 'pre_get_document_title', '' );
if ( ! empty( $title ) ) {
return $title;
}
所以,我把你的過濾器和鉤子改為 pre_get_document_title 。它沒有工作。然後,我將優先順序更改為比 Yoast 更高的級別。然後它工作。所以,我不知道你的 set-up,但我建議你試試這個:
add_filter( 'pre_get_document_title', function( $title )
{
error_log('here');
return $title;
}, 999, 1 );
次佳解決方案
從頂部到底部以及從底部到頂部讀取您的帖子後,您都可以使用透過 pre_get_document_title 過濾器傳遞標題的過濾器。這裡的線索如下:
I’ve also tried
pre_get_document_title, which does fire on page load,
看看 wp_get_document_title()的 soure 程式碼,我們看到以下程式碼:
$title = apply_filters( 'pre_get_document_title', '' );
if ( ! empty( $title ) ) {
return $title;
}
這意味著,每當一個非空值透過 pre_get_document_title 過濾器時,wp_get_document_title()函式將返回透過 pre_get_document_title 過濾器傳遞的任何值。在這種情況下,document_title_separator 過濾器和 document_title_parts 過濾器將不會執行,因為它們僅在 pre_get_document_title 過濾器之後執行。
看看你說的更進一步:
…though I’m unable to get it to actually change the title.
您絕對有一個具有許可權的 pre_get_document_title 過濾器,該過濾器覆蓋了同一過濾器的例項,並且由於此過濾器,該函式將返回傳遞給它的任何結果,導致 document_title_parts 過濾器未被執行。
您將需要做的是使用 grep 或一個好的編輯器,並搜尋整個 wp-content 資料夾中的 pre_get_document_title 過濾器。找到該過濾器後,您可以從中刪除該過濾器,並將其替換為自己的過濾器
第三種解決方案
經過一些實驗,我來到了以下建議:可以這樣,<title> 標籤是你父母主題的 header.php 中的”hard coded” 嗎?如果是這種情況,您可以嘗試從您的孩子主題的 header.php(將您的父母的 header.php 複製到您的子主題資料夾) 中刪除<title> 標籤,然後透過 functions.php 新增主題支援:
add_theme_support( 'title-tag' );
我會嘗試解釋什麼導致我這個建議:我嘗試像你和其他人建議 – 但事實證明,我在原始碼中找到了兩個<title> 標籤。第一個有標準標題,第二個是修改標題。但是 (當然) 在瀏覽器標題欄我只能看到預設標題。
然後我檢查了我使用的父主題的 header.php(二十五),而<title> 標籤確實是硬編碼的模板,如下所示:
<title><?php wp_title( '|', true, 'right' ); ?></title>
我刪除它之後,我新增了以下程式碼到孩子主題的 functions.php 它的工作:
/**
* Theme support added
*/
function add_theme_support_child() {
add_theme_support( 'title-tag' );
}
add_action( 'after_setup_theme', 'add_theme_support_child', 11 );
/**
* Change the title of a page
*
*/
function change_title_for_a_template( $title ) {
// Check if current page template is 'template-homepage.php'
// if ( is_page_template( 'template-homepage.php' ) ) {
// change title parts here
$title['title'] = 'My Title';
$title['tagline'] = 'My fancy tagline'; // optional
$title['site'] = 'example.org'; //optional
// }
return $title;
}
add_filter( 'document_title_parts', 'change_title_for_a_template', 10, 1 );
所以它基本上也是在從模板中刪除<title> 標籤之前進行的,只有當時有兩個被忽略的<title> 標籤。這可能與您的主題相同嗎?
因為 wp 4.4.0 但是<title> 標籤是由_wp_render_title_tag()函式動態建立的,它基本上呼叫另一個函式 wp_get_document_title()並將 html 標籤包裹在結果周圍。長篇小說:如果您的主題 header.php 缺少<title> 標籤,您可以透過 pre_get_document_title 或 document_title_parts 直接覆蓋標題,如 here 所示:
1) 直接更改標題:
add_filter('pre_get_document_title', 'change_the_title');
function change_the_title() {
return 'The expected title';
}
2) 過濾標題部分:
add_filter('document_title_parts', 'filter_title_part');
function filter_title_part($title) {
return array('a', 'b', 'c');
}
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。