使用WordPress 5.5,主題作者現(xiàn)在將能夠輕松過濾存檔頁面標題,因此他們可以使用自己的HTML標記。
以前,現(xiàn)有的get_the_archive_title掛鉤僅限于過濾存檔頁面上使用的整個標題字符串。一些貢獻者指出,此過濾器不夠具體,無法滿足所有主題開發(fā)人員對存檔頁面標題相關的需求。
此次對??get_the_archive_title()?更改中,將內(nèi)部$title?變量拆分為??$title?和?$prefix。
通過使用新的??get_the_archive_title_prefix?過濾鉤子,前綴現(xiàn)在可以包裝在自定義元素中或完全刪除。
基本示例
get_the_archive_title_prefix可用于過濾存檔頁面標題前綴。它僅接受一個參數(shù)$prefix,這是一個包含標題的文本前綴的字符串。
要將存檔標題前綴替換為另一文本,請使用:
function mytheme_archive_title_prefix( $prefix ){
$prefix = __( 'Currently viewing archives for:', 'my-theme' );
return $prefix;
}
add_filter( 'get_the_archive_title_prefix', 'mytheme_archive_title_prefix' );
要完全刪除存檔標題前綴,請使用:
add_filter( 'get_the_archive_title_prefix', '__return_empty_string' );
此更改還將標題部分包裝為一個??span?元素,并將原始標題和前綴傳遞給現(xiàn)有??get_the_archive_title?過濾器,從而允許對存檔標題進行進一步的自定義。
該過濾器傳遞以下參數(shù):
$title:要顯示的存檔標題$original_title:無前綴的存檔標題$prefix:存檔標題前綴
這是一個使用示例:
function mytheme_get_the_archive_title( $title, $original_title, $prefix ) {
$prefix = '<span class="archive-prefix">' . __( 'Currently viewing archives for:', 'my-theme' ) . '</span>';
$title = '<span class="archive-title">' . $original_title . '</span>';
return $prefix . $title;
}
add_filter( 'get_the_archive_title', 'mytheme_get_the_archive_title', 10, 3 );
有關此更改的完整說明,請參見相關的頁面:#31237,#38545和#42768。




