Sample Content:
<h2 id="h2-1">H2 Heading (1)</h2>
<p>Lorem ipsum dolor sit amet consectetur ...</p>
<h3 id="h3-1">H3 Heading</h3>
<h2 id="h2-2">H2 Heading (2)</h2>
What I'm trying to achieve:
<ul>
<li><a href='#h2-1'>H2 Heading (1)</a></
<li><a href='#h2-2'>H2 Heading (2)</a></li>
</ul>
I knew it can be done with a regex function but I don't know how to finish the function. Here's what I have done so far:
function table_of_contents() {
$content = get_post_field( 'post_content', $post->ID );
$tags = preg_match_all( '#<h2>(.*?)</h2>#', $content, $matches );
return $tags;
}