admin管理员组文章数量:1023773
I am trying to change the color to red on certain words on my site. However the below code changes each word to "array".
Can someone tell me what is wrong with my code?
function replace_content($content)
{
$newwords = array("Check", "Map", "Comments", "Send", "Print");
$content = str_replace($newwords, '<span style="color:red">' . $newwords . '</span>', $content);
return $content;
}
add_filter('the_content','replace_content');
I am trying to change the color to red on certain words on my site. However the below code changes each word to "array".
Can someone tell me what is wrong with my code?
function replace_content($content)
{
$newwords = array("Check", "Map", "Comments", "Send", "Print");
$content = str_replace($newwords, '<span style="color:red">' . $newwords . '</span>', $content);
return $content;
}
add_filter('the_content','replace_content');
Share
Improve this question
edited Dec 26, 2016 at 5:28
Tunji
2,9611 gold badge18 silver badges28 bronze badges
asked Sep 19, 2015 at 19:58
BrendanBrendan
1332 silver badges8 bronze badges
2
|
1 Answer
Reset to default 2function replace_content($text) {
$replace = array(
'Check' => '<span style="color:red">Check</span>',
'Map' => '<span style="color:red">Map</span>',
'Comments' => '<span style="color:red">Comments</span>',
'Print' => '<span style="color:red">Print</span>'
);
$text = str_replace(array_keys($replace), $replace, $text);
return $text;
}
add_filter('the_content','replace_content');
I am trying to change the color to red on certain words on my site. However the below code changes each word to "array".
Can someone tell me what is wrong with my code?
function replace_content($content)
{
$newwords = array("Check", "Map", "Comments", "Send", "Print");
$content = str_replace($newwords, '<span style="color:red">' . $newwords . '</span>', $content);
return $content;
}
add_filter('the_content','replace_content');
I am trying to change the color to red on certain words on my site. However the below code changes each word to "array".
Can someone tell me what is wrong with my code?
function replace_content($content)
{
$newwords = array("Check", "Map", "Comments", "Send", "Print");
$content = str_replace($newwords, '<span style="color:red">' . $newwords . '</span>', $content);
return $content;
}
add_filter('the_content','replace_content');
Share
Improve this question
edited Dec 26, 2016 at 5:28
Tunji
2,9611 gold badge18 silver badges28 bronze badges
asked Sep 19, 2015 at 19:58
BrendanBrendan
1332 silver badges8 bronze badges
2
-
That happens, because you've put an array into the string:
'<span style="color:red">' . $newwords . '</span>'
. – fuxia ♦ Commented Sep 19, 2015 at 20:02 - Thanks, this led me to further research and I was able to get it to work with this: – Brendan Commented Sep 19, 2015 at 20:37
1 Answer
Reset to default 2function replace_content($text) {
$replace = array(
'Check' => '<span style="color:red">Check</span>',
'Map' => '<span style="color:red">Map</span>',
'Comments' => '<span style="color:red">Comments</span>',
'Print' => '<span style="color:red">Print</span>'
);
$text = str_replace(array_keys($replace), $replace, $text);
return $text;
}
add_filter('the_content','replace_content');
本文标签: filtersstrreplace function in theme
版权声明:本文标题:filters - str_replace function in theme 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745598306a2158304.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
'<span style="color:red">' . $newwords . '</span>'
. – fuxia ♦ Commented Sep 19, 2015 at 20:02