Joe主题文章添加文章目录树

chen'mo
2023-04-01 / 3 评论 / 7,768 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2023年03月31日,已超过363天没有更新,若内容或图片失效,请留言反馈。

前言

去年写过一篇 Joe主题开启文章目录结构 ,最近感觉有点难看,而且默认是隐藏的,每次都得用鼠标点一下。网上找了其他的目录树方案教程,都很老了。设置了都没有效果。参考着别人的网站,折腾了一下午终于弄好了,特此记录下来。

效果

1680247845616.png

修改

post.php

  • 加载js和css

    <!--加在头部-->
    <link rel="stylesheet" href="<?php $this->options->themeUrl('assets/css/jfloor.min.css'); ?>">
    
    <!-- body标签 最下面 -->
    <?php if ($this->options->jfloor === 'on') : ?>
      <!-- 目录树 -->
          <script src="<?php  $this->options->themeUrl('assets/js/jfloor.min.js'); ?>"></script>
    <?php endif; ?>
  • 加载功能代码,大概41行

          <div class="joe_container j-post">
              <section class="j-adaption" style="height: auto !important;">
              <?php if ($this->options->jfloor === 'on') : ?>
                  <?php GetCatalog(); ?>
              <?php endif; ?>
              ...
          </div>

functions.php

后台功能开关

    //开启文章目录树显示
    $jfloor = new Typecho_Widget_Helper_Form_Element_Select(
        'jfloor',
        array(
            'off' => '关闭(默认)',
            'on' => '开启',
        ),
        'off',
        '是否启用文章目录树显示',
        '介绍:开启之后 在文章最左侧显示目录树(手机端不显示)'
    );
    $jfloor->setAttribute('class', 'joe_content joe_post');
    $form->addInput($jfloor->multiMode());

core/function.php

目录树函数,放在文件最后

/*生成文章目录树*/
function CreateCatalog($obj)
{
    global $catalog;
    global $catalog_count;
    $catalog = array();
    $catalog_count = 0;
    $obj = preg_replace_callback('/<h([1-6])(.*?)>(.*?)<\/h\1>/i', function ($obj) {
        global $catalog;
        global $catalog_count;
        $catalog_count++;
        $catalog[] = array('text' => trim(strip_tags($obj[3])), 'depth' => $obj[1], 'count' => $catalog_count);
        return '<h' . $obj[1] . $obj[2] . ' id="cl-' . $catalog_count . '"><span>' . $obj[3] . '</span></h' . $obj[1] . '>';
    }, $obj);
    return $obj;
}

/*获取文章目录树*/
function GetCatalog()
{
    global $catalog;
    $index = '';
    if ($catalog) {
        $index = '<ul>';
        $prev_depth = '';
        $to_depth = 0;
        foreach ($catalog as $catalog_item) {
            $catalog_depth = $catalog_item['depth'];
            if ($prev_depth) {
                if ($catalog_depth == $prev_depth) {
                    $index .= '</li>';
                } elseif ($catalog_depth > $prev_depth) {
                    $to_depth++;
                    $index .= '<ul>';
                } else {
                    $to_depth2 = ($to_depth > ($prev_depth - $catalog_depth)) ? ($prev_depth - $catalog_depth) : $to_depth;
                    if ($to_depth2) {
                        for ($i = 0; $i < $to_depth2; $i++) {
                            $index .= '</li></ul>';
                            $to_depth--;
                        }
                    }
                    $index .= '</li>';
                }
            }
            $index .= '<li><a href="#cl-' . $catalog_item['count'] . '" data-href="#cl-' . $catalog_item['count'] . '">' . $catalog_item['text'] . '</a>';
            $prev_depth = $catalog_item['depth'];
        }
        for ($i = 0; $i <= $to_depth; $i++) {
            $index .= '</li></ul>';
        }
        $index = '<div class="j-floor s-j-floor"><div class="contain" id="jFloor" style="top: 126px;"><div class="title">文章目录</div>' . $index . '<svg class="toc-marker" xmlns="http://www.w3.org/2000/svg"><path stroke="var(--theme)" stroke-width="3" fill="transparent" stroke-dasharray="0, 0, 0, 1000" stroke-linecap="round" stroke-linejoin="round" transform="translate(-0.5, -0.5)" /></svg></div></div>';
    }
    echo $index;
}

core/core.php

文章页面相应位置(初始化函数themeInit)加载,大约第83行

      /* 文章目录树 */
      if ($self->is('single')) {
        $self->content = CreateCatalog($self->content);
      }

需要的资源

文件

  • jfloor.min.css //样式
  • jfloor.min.js //功能实现

下载

本站备份下载

'目录树.rar' 大小:. 1.4 KB | . 下载次数: 165 下载附件

UserLog一款Typecho用户登陆日志插件
« 上一篇 03-31
Joe主题导航栏标签添加角标
下一篇 » 04-02

评论 (3)

插入图片
  1. 头像
    艾霂 Lv.1   安徽省淮北市
    Windows 10 · QQ Browser
    沙发
    此条为密语,发布者可见
    回复
  2. 头像
    博纳 Lv.1   安徽省淮北市
    Windows 10 · QQ Browser
    板凳

    Call to a member function is() on null这什么情况

    回复
    1. 头像
      博纳 Lv.1   安徽省淮北市
      Windows 10 · QQ Browser
      @ 博纳

      应该是文章页面相应位置(初始化函数themeInit)加载有问题怎么解决

      回复