• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • dedecms
  • ecshop
  • z-blog
  • UcHome
  • UCenter
  • drupal
  • WordPress
  • 帝国cms
  • phpcms
  • 动易cms
  • phpwind
  • discuz
  • 科汛cms
  • 风讯cms
  • 建站教程
  • 运营技巧
您的位置:首页 > CMS教程 >WordPress > WordPress免插件插入文章索引/文章目录

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

作者:小兽 字体:[增加 减小] 来源:互联网 时间:2018-11-02

小兽向大家介绍了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;
}
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

相关文章

  • 2018-11-02WordPress防止F12审查元素扒代码的两种方法
  • 2018-11-02wordpress教程之 WordPress 个人档案
  • 2018-11-02WordPress无插件实现主题彩色标签云的N种方法总结
  • 2018-11-02为什么我的wordpress首页没有关键词没有描述都是is_home()惹得祸
  • 2017-05-13Wordpress忘记后台密码怎么办 轻松找回WP密码的方法
  • 2018-11-02wordpress教程之 WordPress添加标签
  • 2018-11-02wordpress如何在文章中插入视频
  • 2018-11-02linux+nginx下安装wordpress出现的一些问题集合
  • 2018-11-02WordPress 如何判断一篇文章是否存在?
  • 2017-05-13WordPress安装图解教程

文章分类

  • dedecms
  • ecshop
  • z-blog
  • UcHome
  • UCenter
  • drupal
  • WordPress
  • 帝国cms
  • phpcms
  • 动易cms
  • phpwind
  • discuz
  • 科汛cms
  • 风讯cms
  • 建站教程
  • 运营技巧

最近更新的内容

    • 新手wordpress企业建站需要注意的7点
    • 新手学习wordpress的学习方法大全
    • WordPress的JavaScript 本地化实现
    • wordpress主题制作涉及到的基本模板及说明整理
    • wordpress编辑器中添加链接功能自动加入nofollow的方法
    • wordpress地图插件神器:Google XML Sitemaps
    • wordpress如何安装,WordPress安装过程
    • 修改wordpress文章发布时间精确到秒
    • wordpress后台的小工具为什么拖拽不动了?
    • 较完美的WordPress文章摘要(截断)方案分享

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有