我找到了一个将关键字更改为链接的脚本。
function wp_affiliate_links($text){
$replace = array(
'/Modkelle/' => ' <a href="http://www.nurseryrhymes.me/">123</a> ',
'/Philips/' => ' <a href="http://www.nurseryrhymes.me/">poems</a> ',
'/Jungs/' => ' <a href="http://www.nurseryrhymes.me/">rhymes</a> ',
);
foreach ( $replace as $key ) {
$text = preg_replace( array_keys($replace), $replace, $text, 1 );
return $text;
}
}
add_filter('the_content', 'wp_affiliate_links');
所以它有效,但问题是该脚本还会更改<img src="/wp-content/uploads/03/Philips-product.jpg"></img>
和<a href="http://www.Philips.de"> </a>
标记内的文本。过滤器可能只会更改<a>
和<img>
标记之外的文字吗?
让<a>
和<img>
标签内的文字不变?
答案 0 :(得分:0)
这个功能应该是这样的:
function wp_affiliate_links($text){
return preg_replace(
array('/Modkelle/', '/Philips/', '/Jungs/' ),
array(' <a href="http://www.nurseryrhymes.me/">123</a> ', ' <a href="http://www.nurseryrhymes.me/">poems</a> ', ' <a href="http://www.nurseryrhymes.me/">rhymes</a> '),
$text
);
}
add_filter('the_content', 'wp_affiliate_links');