在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
我希望帮你。
+1
非常感谢你的解释何塞卡洛斯,这正是我所需要的, –
如果要在指定类别中替换“帖子标题”,是否要替换?或者你想替换所有的“帖子标题”? –