英文:
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);
我遇到的问题是我想提取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并返回基于遇到的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('#<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);
The output is like the picture below where all H2's are pulled and converted to links.
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: <h2 id="0/1/2/3/etc.">
. I'm using this code to try and do that:
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);
The code is somewhat successful, but the problem is that all H2's are returned with id="2"
instead of 0/1/2/3/etc. See the image below of the source code.
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 = <<<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);
Output
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>
See a PHP demo.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论