WordPress无插件实现内容目录索引

WordPress 文章内容索引插件:https://wordpress.org/plugins/table-of-contents-plus/

鉴于WP程序的负载很大,能少使用插件就少使用。那有没有不使用插件可以实现这样的索引功能能?于是我找着找着,终于找到一个博主分享的这样的无插件实现内容索引效果。

[title]修改模板所在的function.php文件[/title]

function article_index($content) {      $matches = array();      $ul_li = '';      $r = "/<h3>([^<]+)</h3>/im";      if(preg_match_all($r, $content, $matches)) {          foreach($matches[1] as $num => $title) {              $content = str_replace($matches[0][$num], '<h4 id="title-'.$num.'">'.$title.'</h4>', $content);              $ul_li .= '<li><a href="#title-'.$num.'" title="'.$title.'">'.$title."</a></li>n";          }          $content = "n<div id="article-index">                  <strong>文章目录</strong>                  <ul id="index-ul">n" . $ul_li . "</ul>              </div>n" . $content;      }      return $content;  }  add_filter( "the_content", "article_index" );

[title]优化界面,修改CSS[/title]

在我们内容编辑的时候,切换到HTML编辑器,然后在需要索引的标题用H3标注,具体的用H几要和上面的定义FUNCTION对应,默认的是H3。然后在显示的时候会自动抓取。为了达到美化的效果,需要用到CSS,具体的还是要根据需求调整。

#article-index {      -moz-border-radius: 6px 6px 6px 6px;      border: 1px solid #DEDFE1;      float: right;      margin: 0 0 15px 15px;      padding: 0 6px;      width: 200px;      line-height: 23px;  }  #article-index strong {      border-bottom: 1px dashed #DDDDDD;      display: block;      line-height: 30px;      padding: 0 4px;  }  #index-ul {      margin: 0;      padding-bottom: 10px;  }  #index-ul li {      background: none repeat scroll 0 0 transparent;      list-style-type: disc;      padding: 0;      margin-left: 20px;  }

腾讯云限时秒杀【点击购买】

搬瓦工,CN2高速线路,1GB带宽,电信联通优化KVM,延迟低,速度快,建站稳定,搬瓦工BandwagonHost VPS优惠码BWH26FXH3HIQ,支持<支付宝> 【点击购买】!

Vultr$3.5日本节点,512M内存/500G流量/1G带宽,电信联通优化,延迟低,速度快【点击购买】!

阿里云香港、新加坡VPS/1核/1G/25G SSD/1T流量/30M带宽/年付¥288【点击购买】

WordPress无插件实现内容目录索引

`微信`扫码 加好友

链接到文章: https://gkxyz.com/wordpresswuchajianshixianneirongmulusuoyin.html

推荐站点

  • Vultr

    Vultr Global Cloud Hosting – Brilliantly Fast SSD […]

  • 新闻中心首页_新浪网

    新浪网新闻中心是新浪网最重要的频道之一,24小时滚动报道国内、国际及社会新闻。每日编发新闻数以万计。

  • 阿里云-为了无法计算的价值

    阿里云——阿里巴巴集团旗下公司,是全球领先的云计算及人工智能科技公司之一。提供免费试用、云服务器、云数据库、云 […]

  • 观察者网

    观察者网,致力于荟萃中外思想者精华,鼓励青年学人探索,建中西文化交流平台,为崛起中的精英提供决策参考。

  • Akamai(Linode)

    Deploy more with Linux virtual machines, global infrast […]

  • 腾讯云 – 产业智变 云启未来

    腾讯云为数百万的企业和开发者提供安全稳定的云计算服务,涵盖云服务器、云数据库、云存储、视频与CDN、域名注册等 […]

评论已关闭