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

如果需要同时匹配 h2 和 h3 标签,推荐使用下面这种,同样还是加入下面代码到主题 functions.php 文件中去function article_index($content) { $matches = array(); $ul_li = ''; //匹配出 h2、h3 标题 $rh = "/<h

如果需要同时匹配 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;}

给TA打赏
共{{data.count}}人
人已打赏
wordpress教程

外贸SOHO如何利用WordPress搭建营销型网站

2023-2-16 15:52:30

wordpress教程

WordPress建站用FTP上传图片怎么在媒体库显示不出来

2023-2-16 15:52:32

【腾讯云】11.11云上盛惠!云服务器首年1.8折起,买1年送3个月!
11.11云上盛惠!海量产品·轻松上云!云服务器首年1.8折起,买1年送3个月!超值优惠,性能稳定,让您的云端之旅更加畅享。
查看更多相关信息>>
站长

(工作日 10:00 - 22:30 为您服务)

2026-05-15 20:31:45

您好,无论是售前、售后、意见建议……均可通过联系工单与我们取得联系。

猜你想问:

  • 购买的模板免费包安装吗?

  • 这个演示地址有吗?

  • 购买vip会员可以下载哪些模板?

您的留言我们已经收到,我们将会尽快跟您联系!
取消
立即选择任一渠道联系我们