您当前的位置: 首页 > CMS教程 > WordPress教程 > wordpress中无插件实现内容目录索引

wordpress中无插件实现内容目录索引

作者:xiaoxiao 来源:未知 发布时间: 2013-10-30 16:47 点击:
如果依靠大量的插件实现各种功能,会增加服务器负担,拖慢页面打开速度,增加网站的安全隐患。 能实现内容目录索引的插件主要有:Auto Anchor List、Content Index for WordPress、WP-TOC、seo Friendly Table of Contents等。用法大同小异。 今天给大家说一下WordPres

wordpress中无插件实现内容目录索引

      如果依靠大量的插件实现各种功能,会增加服务器负担,拖慢页面打开速度,增加网站的安全隐患。
  能实现内容目录索引的插件主要有:Auto Anchor List、Content Index for WordPress、WP-TOC、seo Friendly Table of Contents等。用法大同小异。
  今天给大家说一下WordPress无插件实现内容目录索引。
  工具/原料
  wordpress模板文件。
  富文本编辑器,推荐editplus.
  方法/步骤
  1
  把下面的代码放到模板所在的function.php文件里
  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" );
  2
  在我们内容编辑的时候,切换到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;
  }

分享到:
本文"wordpress中无插件实现内容目录索引"由远航站长收集整理而来,仅供大家学习与参考使用。更多网站制作教程尽在远航站长站。
顶一下
(1)
100%
踩一下
(0)
0%
[点击 次] [返回上一页] [打印]
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
关于本站 - 联系我们 - 网站声明 - 友情连接- 网站地图 - 站点地图 - 返回顶部
Copyright © 2007-2013 www.yhzhan.com(远航站长). All Rights Reserved .
远航站长:为中小站长提供最佳的学习与交流平台,提供网页制作与网站编程等各类网站制作教程.
官方QQ:445490277 网站群:26680406 网站备案号:豫ICP备07500620号-4