WordPress免插件插入文章索引/文章目录

如果需要同时匹配 h2 和 h3 标签,推荐使用下面这种,同样还是加入下面代码到主题 functions.php 文件中去:

function article_index($content) {
    $matches = array();
    $ul_li = '';
    //匹配出 h2、h3 标题
    $rh = "/<h[23]>(.*)<\/h[23]>/im";
    $h2_num = 0;
    $h3_num = 0;
    //判断是否是文章页
    if(is_single()){
         if(preg_match_all($rh, $content, $matches)) {
            // 找到匹配的结果
            foreach($matches[1] as $num => $title) {
                $hx = substr($matches[0][$num], 0, 3);      //前缀,判断是 h2 还是 h3
                $start = stripos($content, $matches[0][$num]);  //匹配每个标题字符串的起始位置
                $end = strlen($matches[0][$num]);       //匹配每个标题字符串的结束位置
                if($hx == "<h2"){
                    $h2_num += 1; //记录 h2 的序列,此效果请查看百度百科中的序号,如 1.1、1.2 中的第一位数
                    $h3_num = 0;
                    // 文章标题添加 id,便于目录导航的点击定位
                    $content = substr_replace($content, '<h2 id="h2-'.$num.'">'.$title.'</h2>',$start,$end);
                    $title = preg_replace('/<.+>/', "", $title); //将 h2 里面的 a 链接或者其他标签去除,留下文字
                    $ul_li .= '<li class="h2_nav"><a href="#h2-'.$num.'" class="tooltip" title="'.$title.'">'.$title."</a></li>\n";
                }else if($hx == "<h3"){
                    $h3_num += 1; //记录 h3 的序列,此熬过请查看百度百科中的序号,如 1.1、1.2 中的第二位数
                    $content = substr_replace($content, '<h3 id="h3-'.$num.'">'.$title.'</h3>',$start,$end);
                    $title = preg_replace('/<.+>/', "", $title); //将 h3 里面的 a 链接或者其他标签去除,留下文字
                    $ul_li .= '<li class="h3_nav"><a href="#h3-'.$num.'" class="tooltip" title="'.$title.'">'.$title."</a></li>\n";
                }   
            }
        }
        // 将目录拼接到文章
        $content =  $content . "<div class=\"post_nav\"><ul class=\"post_nav_content\">\n" . $ul_li . "</ul></div>\n";
        return $content;
    }elseif(is_home){
        return $content;
    }
}
add_filter( "the_content", "article_index" );

如果需要指定文章加入文章索引/文章目录,可以将上面的判断语句按需修改。

同样的需要加入 css 文件:

/*目录效果*/
.post_nav {
    position: fixed;
    right: 50%;
    top: 50%;
    margin-top: -24px;
    z-index: 20;
    background: #FFF;
    width: 184px;
    display: block;
    overflow: hidden;
    margin-right: 624px;
}
.post_nav .post_nav_side {
    top: 0;
    width: 0;
    min-height: 40px;
    background-color: #eaeaea;
    border: 1px solid #eaeaea;
    border-top: 0;
    border-bottom: 0;
    position: absolute;
    left: 5px;
}
.post_nav .post_nav_content {
    height: auto;
    padding-left: 18px;
    overflow: hidden;
    margin: 20px 0;
    position: relative;
    max-height: 520px;
    text-indent: 0;
    overflow-y: scroll;
    list-style:none;
}
.post_nav .post_nav_content li{
    line-height:22px;
    height:22px;
}
.post_nav_content li a.tooltip {
opacity: 100 !important;
}
.post_nav .post_nav_content li a {
    display: inline-block;
    vertical-align: top;
    max-width: 146px;
    height: 22px;
    overflow: hidden;
    -webkit-text-overflow: ellipsis;
    color: #333;
    text-decoration: none;
}