使用 the_title 过滤器挂钩向所有帖子标题添加单词
我想在我的 WordPress 网站上所有帖子标题的开头添加一个“单词”。
我正在使用以下代码,它工作正常除了它还将该单词添加到菜单标题中。你们能帮忙修改一下代码,这样就不会在菜单标题中添加这个词吗?
add_filter( 'the_title', function( $title, $id ) {
return 'My World' . $title ;
}, 10, 2 );
I want to add a "word" to the beginning of all post titles on my WordPress website..
I am using the following code and it's working fine except that it also adds the word to menu titles. Can you guys please help in modifying the code so it won't add the word to menu titles?
add_filter( 'the_title', function( $title, $id ) {
return 'My World' . $title ;
}, 10, 2 );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,为了仅将该单词添加到帖子标题(例如,而不是页面),请将代码更改为:
基本上,我们使用 get_post_type() 函数检查标题是否属于帖子,如果是,则添加“我的世界”文本,否则该函数将返回原标题。
就我个人而言,我还会使用 is_admin() 检查此代码是否仅在前端运行 功能,以便帖子标题正常显示在管理部分(WP Dashboard > Posts > All Posts):
So in order to only add that word to post titles (and not pages, for example), change your code to this:
Basically, we're using the get_post_type() function to check that the title belongs to a post and if so then we add the 'My World' text to it, otherwise the function will return the original title.
Personally I would also check that this code only runs on the front-end by using the is_admin() function so post titles are displayed normally on the admin section (WP Dashboard > Posts > All Posts):