如何为所有”h2标签”添加数字

我有一个Wordrpess网站。我想为所有帖子在” h2标签” 中添加一个数字
【如何为所有” h2标签” 添加数字】我的帖子格式:

text< h2> text< /h2> text< h2> text< /h2> text< h2> text< /h2> ... ...

我要这个:
text< h2> NUMBER-1 text< /h2> text< h2> NUMBER-2 text< /h2> text< h2> NUMBER-3 text< /h2> ... ...

#1最简单的方法是使用WordPress帖子内容过滤器。
可以通过以下方式调用过滤器:
function my_the_content_filter($content) { // do what you want to the content return $content; } add_filter( 'the_content', 'my_the_content_filter' );

尽管使用Regex来处理HTML并不是很好, 但是在这种情况下, 这很简单, 你可以使用preg_match查找所有标签, 然后在它们后面插入数字。
#2我认为这就是你要寻找的…
在下面的代码中, 我使用一个for循环将花括号中的代码执行一定次数。第一个$ x初始化循环计数器值, $ x < = $ number_of_items; 设置运行块的次数, $ x ++递增初始$ x。然后, 我将$ x值插入代码中。
有关PHP for循环的更多信息, 请访问-https://www.w3schools.com/php/php_looping_for.asp
$number_of_items = 5; for ($x = 0; $x < = $number_of_items; $x++) { echo "< h2> NUMBER-" . $x . " text< /h2> "; }

    推荐阅读