如何根据H2标签的数量返回基于生成的ID的WordPress内容

huangapple go评论59阅读模式
英文:

How can I return WordPress content with generated ID's based on the number of H2 tags

问题

以下是您要翻译的内容:

"我试图做的是创建一个代码,从the_content中提取并读取所有H2标签,以创建一个内容表格。我已经成功地使用下面的代码完成了这个任务:

function h2s_in_the_content($content) {
    if (preg_match_all('#<h2>(.*?)</h2>#', $content, $matches)) {
        foreach ($matches[1] as $key => $value) {
			
			echo '<li class="inhoudsopgave-li">';
			echo '<a href="#' . $key . '" class="link white">' . $value . '</a>';
			echo '</li>';
        }
    }
}

add_filter('the_content', 'h2s_in_the_content', 98);

输出结果如下图所示,所有H2标签都被提取并转换为链接。
如何根据H2标签的数量返回基于生成的ID的WordPress内容

我遇到的问题是我想提取the_content,并以与上面相同的方式读取和替换所有H2标签,然后使用$key变量返回它们,应该输出如下:<h2 id="0/1/2/3/etc.">。我正在尝试使用以下代码来实现这一目标:

function replace_text_wps($content){
	if (preg_match_all('#<h2>(.*?)</h2>#', $content, $matches)) {
		foreach ($matches[1] as $key => $value) {
			$replace = array(
				'<h2>' => '<h2 id="' . $key . '">'
			);
		return $content;
		}
	}
    $content = str_replace(array_keys($replace), $replace, $content);
	return $content;
}

add_filter('the_content', 'replace_text_wps',99);

这段代码在某种程度上成功了,但问题是所有H2标签都以id="2"返回,而不是0/1/2/3/etc。请参见下面的源代码图像。

如何根据H2标签的数量返回基于生成的ID的WordPress内容

我的代码有什么问题,如何使其计算每个H2并返回基于遇到的H2数量的ID。在第一个示例中,提取H2并更改为链接时可以正常工作,但在第二个示例中不行。"

英文:

What I'm trying to do it make a code where all H2 tags from the_content are pulled and read out to create a table of content. I've done this succesfully with the code below:

function h2s_in_the_content($content) {
    if (preg_match_all(&#39;#&lt;h2&gt;(.*?)&lt;/h2&gt;#&#39;, $content, $matches)) {
        foreach ($matches[1] as $key =&gt; $value) {
			
			echo &#39;&lt;li class=&quot;inhoudsopgave-li&quot;&gt;&#39;;
			echo &#39;&lt;a href=&quot;#&#39; . $key . &#39;&quot; class=&quot;link white&quot;&gt;&#39; . $value . &#39;&lt;/a&gt;&#39;;
			echo &#39;&lt;/li&gt;&#39;;
        }
    }
}

add_filter(&#39;the_content&#39;, &#39;h2s_in_the_content&#39;, 98);

The output is like the picture below where all H2's are pulled and converted to links.
如何根据H2标签的数量返回基于生成的ID的WordPress内容

The problem I'm encountering is that I want to fetch the_content, read and replace all H2's on the same way as above and return them with the $key variable which should output this: &lt;h2 id=&quot;0/1/2/3/etc.&quot;&gt;. I'm using this code to try and do that:

function replace_text_wps($content){
	if (preg_match_all(&#39;#&lt;h2&gt;(.*?)&lt;/h2&gt;#&#39;, $content, $matches)) {
		foreach ($matches[1] as $key =&gt; $value) {
			$replace = array(
				&#39;&lt;h2&gt;&#39; =&gt; &#39;&lt;h2 id=&quot;&#39; . $key . &#39;&quot;&gt;&#39;
			);
		return $content;
		}
	}
    $content = str_replace(array_keys($replace), $replace, $content);
	return $content;
}

add_filter(&#39;the_content&#39;, &#39;replace_text_wps&#39;,99);

The code is somewhat successful, but the problem is that all H2's are returned with id=&quot;2&quot; instead of 0/1/2/3/etc. See the image below of the source code.

如何根据H2标签的数量返回基于生成的ID的WordPress内容

What is wrong in my code and how can I make it so that it counts each H2 and returns the id based on the number of the H2 encountered. It works in the first example where the H2's are pulled and changed to links but not in the second example..

Thanks a lot for helping!

答案1

得分: 1

如果您想进行自定义替换,可以跟踪函数触发的次数,并使用preg_replace_callback

$html = <<<HTML
This is <h2>H2 nummer 1</h2>
and this is This is <h2>H2 nummer 2</h2>
This is <h2>H2 nummer 3</h2>
HTML;

function replace_text_wps($content)
{

    return preg_replace_callback('#<h2>(.*?)</h2>#', function ($m) {
        static $h2Count = 0;
        return sprintf('<h2 id="%s">%s</h2>', ++$h2Count, $m[1]);
    }, $content);
}

echo replace_text_wps($html);

输出:

This is <h2 id="1">H2 nummer 1</h2>
and this is This is <h2 id="2">H2 nummer 2</h2>
This is <h2 id="3">H2 nummer 3</h2>

查看PHP演示

英文:

If you want to do a custom replacement, you could keep track of the number of times the function is triggered, and use preg_replace_callback:

$html = &lt;&lt;&lt;HTML
This is &lt;h2&gt;H2 nummer 1&lt;/h2&gt;
and this is This is &lt;h2&gt;H2 nummer 2&lt;/h2&gt;
This is &lt;h2&gt;H2 nummer 3&lt;/h2&gt;
HTML;

function replace_text_wps($content)
{

    return preg_replace_callback(&#39;#&lt;h2&gt;(.*?)&lt;/h2&gt;#&#39;, function ($m) {
        static $h2Count = 0;
        return sprintf(&#39;&lt;h2 id=&quot;%s&quot;&gt;%s&lt;/h2&gt;&#39;, ++$h2Count, $m[1]);
    }, $content);
}

echo replace_text_wps($html);

Output

This is &lt;h2 id=&quot;1&quot;&gt;H2 nummer 1&lt;/h2&gt;
and this is This is &lt;h2 id=&quot;2&quot;&gt;H2 nummer 2&lt;/h2&gt;
This is &lt;h2 id=&quot;3&quot;&gt;H2 nummer 3&lt;/h2&gt;

See a PHP demo.

huangapple
  • 本文由 发表于 2023年5月29日 23:10:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358446.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定