Wordpress过滤the_content - 自动链接脚本

时间:2016-03-12 21:16:35

标签: php jquery html wordpress

我找到了一个将关键字更改为链接的脚本。

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>标签内的文字不变?

1 个答案:

答案 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');