示例帖子
<p>Lorem ipsum dolor sit amet</p>
<p>Morbi elementum odio vel tortor adipiscing vel tempor risus ullamcorper.</p>
我所做的:
function first_paragraph($content){
return preg_replace(\'/<p([^>]+)?>/\', \'<p$1 class="first-paragraph">\', $content, 1);
}
add_filter(\'the_content\', \'first_paragraph\');
会有不同的
class
对于第一段
现在我要添加<span class="first-letter">L</span>
结果应该是
<p class="first-paragraph"><span class="first-letter">L</span>orem ipsum dolor sit amet</p>
<p>Morbi elementum odio vel tortor adipiscing vel tempor risus ullamcorper.</p>
让我知道
<小时>
Update:
我成功了,但不是一个完美的解决方案。
function first_paragraph($content){
$content = substr_replace($content, \'</span>\', 4, 0);
$content = substr_replace($content, \'<span class="first-letter">\', 3, 0);
$content = preg_replace(\'/<p([^>]+)?>/\', \'<p$1 class="first-paragraph">\', $content, 1);
return $content;
}
add_filter(\'the_content\', \'first_paragraph\');