我想将标题中的第一个单词替换为 <span></span>
里面。
Wordpress 标题示例
<h2 class="entry-title"><a href="#">Welcome to Wordpress</a></h2>
我想成为这样的人
<h2 class="entry-title"><a href="#"><span>Welcome</span> to Wordpress</a></h2>
函数
function span_on_title($span) {
return preg_replace('', '', $span, 1);
}
add_filter('the_title','span_on_title');
我可以知道在 preg_replace
上放什么吗?
最佳答案
$title = '<h2 class="entry-title"><a href="#">Welcome to Wordpress</a></h2>';
$title = preg_replace('/<a([^>]+)>([a-zA-Z]+)\s/i', '<a$1><span>$2</span> ', $title);
return $title;
关于php - 你如何编写一个 Wordpress 函数来将 Span 放在标题的第一个单词周围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3507003/