function add_content_after_h2($content){
if (is_single()) {
$div = "This content is after first h2 tag";
$content = preg_replace('/(<\/h2>)/i', '\1'.$div, $content);
}
return $content;
}
add_filter('the_content', 'add_content_after_h2');
使用此代码,内容正在考虑所有H2标签,但我只需要考虑第一个h2标签。
答案 0 :(得分:2)
您需要更改preg_replace()
,如下所示: -
<?php
$div = "This content is after first </h2> tag";
echo $content = preg_replace('/(h2 | <h2> | <\/h2>)/','', $div, 1);
输出: - https://eval.in/715312