function replace_text_wps($text){
$replace = array(
// 'WORD TO REPLACE' => 'REPLACE WORD WITH THIS'
'wordpress' => '<a href="#">wordpress</a>',
'excerpt' => '<a href="#">excerpt</a>',
'function' => '<a href="#">function</a>'
);
$text = str_replace(array_keys($replace),
$replace, $text);
return $text; }
add_filter('the_content','replace_text_wps');
add_filter('the_excerpt','replace_text_wps');
这段代码用来替换一些单词,为什么他使用add_filter()函数两次,是不是错了?
此外,$text = str_replace(array_keys($replace), $replace, $text)
行是什么意思?
答案 0 :(得分:1)
$text = str_replace(array_keys($replace), $replace, $text);
用$ text string中的$ replace替换所有给定的键
这段代码只是过滤了两个不同的字符串。
答案 1 :(得分:1)
$text = str_replace(array_keys($replace), $replace, $text);
此行搜索来自$replace
的所有数组键,并将其替换为各自的值。
foreach($replace as $s => $r) $text = str_replace($s, $r, $text);
答案 2 :(得分:1)
add_filter('the_content','replace_text_wps');
add_filter('the_excerpt','replace_text_wps');
他正在将过滤器应用于帖子的内容以及摘录(通常与帖子主体分开。单独填写)。一般来说,你只在博客列表中使用其中一个,所以他通过将它应用于两者来覆盖他的所有基础。
$text = str_replace(array_keys($replace), $replace, $text);
// 'WORD TO REPLACE' => 'REPLACE WORD WITH THIS'
然后,他只是做了一个字符串替换:http://php.net/manual/en/function.str-replace.php
基本上,如果您的帖子内容包含以下任何单词wordpress, excerpt, excerpt
,则会将该单词替换为包含该单词的链接。