<?php
function ribs_article_nav($content) {
$matches = array();
$ul_li = '';
$respond = '';
$h2 = "/<h2>(.*?)<\/h2>/im";
$h3 = "/<h3>(.*?)<\/h3>/im";
if (preg_match_all($h3, $content, $matches)) {
foreach ($matches[1] as $num => $title) {
$content = str_replace($matches[0][$num], '<h3 id="article_nav_h3_' . $num . '">' . $title . '</h3>', $content);
$ul_li.= '<li><a href="#article_nav_h3_' . $num . '" title="' . $title . '">' . $title . "</a></li>";
}
if (comments_open() || get_comments_number()) {
$respond = '<li><a href="#respond">发表评论</a></li>';
}
if (is_singular()) {
$content = '<div id="ribs_article_nav" class="p-3 border"><div id="article_nav_title">文章大纲</div><ul>' . $ul_li . $respond . '</ul></div>' . $content;
}
}
return $content;
}
add_filter("the_content", "ribs_article_nav");