您不能在str\\u replace中通过替换的限制数量吗?就像那样
$text = str_replace(array_keys($replace), $replace, $text, 1);
EDIT:
对不起,我在想别的事情,错过了这个。您可以使用preg\\u replace并在其中设置限制。就像这个。
function wp_affiliate_links($text){
$replace = array(
\'/ nursery rhymes /\' => \' <a href="http://www.nurseryrhymes.me/">nursery rhymes</a> \',
\'/ poems /\' => \' <a href="http://www.nurseryrhymes.me/">poems</a> \',
\'/ rhymes /\' => \' <a href="http://www.nurseryrhymes.me/">rhymes</a> \',
);
$text = preg_replace( array_keys($replace), $replace, $text, 1 );
return $text;
}
add_filter(\'the_content\', \'wp_affiliate_links\');
add_filter(\'the_excerpt\', \'wp_affiliate_links\');
EDIT 2:
试试这个。
function wp_affiliate_links($text){
$replace = array(
\'/ nursery rhymes /\' => \' <a href="http://www.nurseryrhymes.me/">nursery rhymes</a> \',
\'/ poems /\' => \' <a href="http://www.nurseryrhymes.me/">poems</a> \',
\'/ rhymes /\' => \' <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\');
add_filter(\'the_excerpt\', \'wp_affiliate_links\');