【发布时间】:2021-05-19 02:27:18
【问题描述】:
如果您必须在任何页面中替换或翻译 WordPress 中的一个字符串或文本,functions.php 中的这段代码可以很好地工作:
//The filters.. both are required.
add_filter('gettext', 'change_cancellation_btn');
add_filter('ngettext', 'change_cancellation_btn');
//function
function change_cancellation_btn($cancellation_btn){
$cancellation_btn = str_ireplace('text to replace', 'new text', $cancellation_btn);
return $cancellation_btn;
}
如何使用相同的代码替换多个字符串或文本?
在我的例子中,我用两个不同的按钮替换了文本,为此我使用了两次相同的代码:
//The filters.. both are required.
add_filter('gettext', 'change_cancellation_btn');
add_filter('ngettext', 'change_cancellation_btn');
//function button 1
function change_cancellation_btn($cancellation_btn){
$cancellation_btn = str_ireplace('text 1 to replace', 'new text 1', $cancellation_btn);
return $cancellation_btn;
}
//The filters.. both are required.
add_filter('gettext', 'change_nocancellation_btn');
add_filter('ngettext', 'change_nocancellation_btn');
//function button 2
function change_nocancellation_btn($nocancellation_btn){
$nocancellation_btn = str_ireplace('text 2 to replace', 'new text 2', $nocancellation_btn);
return $nocancellation_btn;
}
那么,有没有办法用一个函数替换多个字符串?
【问题讨论】:
-
str_ireplace() 可以接受数组,见手册。
-
谢谢,我明白了,但是我对php不是很熟悉
标签: php wordpress string function replace