Typecho主题增加文章目录树功能(typecho目录插件)

注意事项

本教程需要改的东西众多 请各位备份好主题文件 以免失败无法恢复!
本次教程以Joe7.7.1版本 为基础 7.3.6按道理也能用
Typecho主题增加文章目录树功能(typecho目录插件) 第2张插图

教程开始

1.首先找到主题的function.php文件
这里我以Joe主题为例 各个主题的不相同 请自行判断
在/Joe/post.php内添加以下代码 大概41行

<?php if ($this->options->jfloor === 'on') : ?>
    <?php GetCatalog(); ?>
<?php endif; ?>

2.Joe主题的function.php文件在/Joe/core/function.php在 大概290行添加以下代码

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'] . '-h'.$catalog_item['depth'].'</a>';
            $prev_depth = $catalog_item['depth'];
        }
        for ($i = 0; $i <= $to_depth; $i++) {
            $index .= '</li></ul>';
        }
        $index = '<div class="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;
}

3.然后在/Joe/core/core.php文件
大概83行 添加以下代码

  if ($self->is('single')) {
    $self->content = CreateCatalog($self->content);
  }

4.再然后在/Joe/function.php文件
大概920行 添加外观后台设置开关

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

5.解压所需的文件
将以下压缩包解压到/Joe/assets目录内
解压完 然后打开post.php
引用解压的文件 jfloor.min.css以及jfloor.min.js

在头部引用css
    <link href="<?php _getAssets('assets/css/jfloor.min.css'); ?>" rel="stylesheet">
在底部引用js
<?php if ($this->options->jfloor === 'on') : ?>
<!-- 目录树 -->
    <script src="<?php _getAssets('assets/js/jfloor.min.js'); ?>"></script>
<?php endif; ?>
Typecho主题增加文章目录树功能(typecho目录插件) 第3张插图文件下载
Joe目录树.zip
PHP/HTML
21K
蓝奏云
更新时间:2023-03-13 22:02:40
免责声明

本站提供的一切软件、教程和内容信息仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络收集整理,如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解!
如若转载,请注明出处:https://www.zxki.cn/5851.html

上一篇 2023-3-13 18:20
下一篇 2023-3-14 01:00

相关推荐

发表评论

为了防止灌水评论,登录后即可评论!

已有 1 条评论