通过wordpress过滤器更改内容中的许多单词

时间:2018-04-18 05:29:37

标签: php wordpress filter

我使用wordpress过滤器通过此代码更改内容中的多个单词

function upperworld ($content){

    $string1 = array('1' => 'BA YEU CON', '2' => 'CON YEU ME' );
    $string = array('1' => 'abc', '2' => 'ccb' );

    foreach ($string as $key => $thay) {
        foreach ($string1 as $key => $canthay) {
            $content = str_replace($thay, $canthay, $content)
        }
    }
    return $content;
}
add_filter( 'the_content', 'upperworld' );

它不起作用,请帮我解决它

2 个答案:

答案 0 :(得分:0)

只需尝试以下代码即可更改the_content()

中的字词
function upperworld ($content){
    $string1 = array('1' => 'BA YEU CON', '2' => 'CON YEU ME' );
    $string = array('1' => 'abc', '2' => 'ccb' );
    foreach ($string1 as $key => $canthay) {
        $thay = $string[$key];
            $content = str_replace($thay, $canthay, $content);
    }
    return $content;
}
add_filter( 'the_content', 'upperworld' );

答案 1 :(得分:0)

您错过了;

声明中的$content = str_replace($thay, $canthay, $content);

通过此

更新您的代码
foreach ($string as $key => $thay) {
    foreach ($string1 as $key => $canthay) {
        $content = str_replace($thay, $canthay, $content);
    }
}

OR

$content = preg_replace(array_map(function($row){return "/{$row}/";}, $string), $string1, $content);