问题描述
我需要以特定方式显示当前帖子的标题.
I need to show the title of the current post in a particular way.
帖子标题如下:
巴鲁巴诺 24
巴鲁巴诺 25
巴鲁巴诺 26
等等...
我只想显示:
巴尔.24
巴尔.25
巴尔.26
总而言之,只需将字符 ubano
替换为 dot
.Balubano 176 变成 Bal.176
In summary, just replace the characters ubano
with a dot
. Balubano 176 becomes Bal. 176
注意:我不想编辑数据库,只是在调用它时更改它在模板中的显示方式.
Note: I don't want to edit the database, just change how it appears in the template when I call it.
如何编辑以下内容以实现我的目标?
How can I edit the following in order to achieve my goal?
<?php echo get_the_title(); ?>
推荐答案
您可以使用过滤器更改标题的显示方式.
You can alter the way titles are displayed by using a filter.
您使用的函数 get_the_title()
通过 the_title
过滤器运行.
The function you're using, get_the_title()
, is run through the the_title
filter.
要修改输出,请将以下代码添加到您的 functions.php 文件中:
To modify the output add the following code to your functions.php file:
/**
* Abbreviate 'Balubano' in post titles.
*
* @param string $title Post title.
* @return string
*/
function wpse_filter_post_titles( $title ) {
return str_replace( 'Balubano', 'Bal.', $title );
}
add_filter( 'the_title', 'wpse_filter_post_titles' );
您也可以将 echo get_the_title()
缩短为 the_title()
.
You can also shorten echo get_the_title()
to just the_title()
.
这篇关于替换 WordPress 帖子标题中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!