我是一位新開發人員,我剛剛在WordPress代碼參考網站上閱讀了the_title()
已被棄用,應該使用get_the_category_by_ID
。
問題是,我無法找到一種方法來取代它,以便它成功地在每篇博客文章上顯示標題。在這些例子中,它似乎只用於類別。在WordPress中,如何用get_the_category_by_ID()替換the_title()?
有人能給我一個如何正確地做到這一點的例子嗎?
我是一位新開發人員,我剛剛在WordPress代碼參考網站上閱讀了the_title()
已被棄用,應該使用get_the_category_by_ID
。
問題是,我無法找到一種方法來取代它,以便它成功地在每篇博客文章上顯示標題。在這些例子中,它似乎只用於類別。在WordPress中,如何用get_the_category_by_ID()替換the_title()?
有人能給我一個如何正確地做到這一點的例子嗎?
您可以使用wp鉤子替換標題。你可以把這個代碼放在你的主題上,或者放在function.php裏面。或者你的插件例如:
function my_custom_replace_title($title, $id = null) {
/* Put here your condition, you get the post ID on "$id", and the current title on "$title"*/
// by category
//if (in_category('XXX', $id)) {
// return '';
//}
//if ($title === 'custom text'){
// return 'this is my new title';
// }
$title = 'My Custom Title - Forced'; // This is to test it.
return $title;
}
add_filter('the_title', 'my_custom_replace_title', 10, 2);
我建議閱讀https://codex.wordpress.org/Plugin_API/Filter_Reference/the_title
我希望幫你。
非常感謝你的解釋何塞卡洛斯,這正是我所需要的, –
如果要在指定類別中替換「帖子標題」,是否要替換?或者你想替換所有的「帖子標題」? –