B2主题美化 主题文章创建内容目录

本文主要针对B2主题的纯代码方法

第1步 :为文章创建目录

打开主题functions.php文件或者在网站后台转到外观>主题>编辑器>functions.php。

然后,将以下代码添加到functions.php文件中。

function create_mg($html) {
    $mg = '';
    if (is_single()) {
        if (!$html) return $html;
        $dom = new DOMDocument();
        libxml_use_internal_errors(true);
        $dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
        libxml_clear_errors();
        $mg = '<div class="mg-bound"><div class="mg-bound__hover-block"><i class="b2font b2-file-list-2-line"></i><p>目录</p></div><ul class="mg-bound__popover">';//向目录添加标题并将其显示在此之上。您可以将文本“目录”替换为您希望在标题中出现的任何内容。
        $h2_status = 0;
        $h3_status = 0;
        $i = 1;
        foreach($dom->getElementsByTagName('*') as $element) {
            if($element->tagName == 'h2') {
                if($h3_status){
                    $mg .= '</ul>';
                    $h3_status = 0;
                    }
                 if($h2_status){
                    $mg .= '</li>';
                    $h2_status = 0;
                  }
                  $h2_status = 1;
                  $mg .= '<li><a href="' . get_the_permalink() . '#mg-' . $i . '">' . $element->textContent . '</a>';//单击后立即创建指向文章中相应部分的跳转链接。
                  $element->setAttribute('id', 'mg-' . $i);
                  $i++;
            }elseif($element->tagName == 'h3') {
                if(!$h3_status){
                    $mg .= '<ul class="mg-sub">';
                    $h3_status = 1;
                }
                $mg .= '<li><a href="' . get_the_permalink() . '#mg-' . $i . '">' . $element->textContent . '</a></li>';
                $element->setAttribute('id', 'mg-' . $i);
                $i++;
            }
        }
        if($h3_status){
            $mg .= '</ul>';
        }
        if($h2_status){
            $mg .= '</li>';
        }
        $mg .= '</ul></div>';
        $html = $dom->saveHTML();
    }
    return $mg . $html;//在目录中的每个标题前添加项目符号。
}
add_filter('the_content', 'create_mg');

第2步 :添加CSS

通过CSS定制,访问主题编辑器的style.css文件。在那里,您需要插入您自己编写的代码以根据需要自定义。

比如我想自定义目录的颜色和强度,所以插入如下代码:

.mg-bound {
    height: 108px;
    box-shadow: 0 2px 8px rgb(0 0 0 / 10%);
    color: #fff;
    position: fixed;
    left: 0;
    font-size: 14px;
    text-align: center;
    z-index: 999999;
}
.mg-bound__hover-block{
    border: none !important;
    position: absolute !important;
    left: 0;
    z-index: 100;
    padding: 0 !important;
    width: 40px;
    height: 108px;
    border-top-right-radius: 4px;
    border-bottom-right-radius: 4px;
    color: #fff;
    background-color: #0066ff;
}
.mg-bound__hover-block i{
    margin-top: 10px;
    display: block;
    font-size: 22px;
}
.mg-bound__hover-block p{
    width: 28px;
    margin-left: 6px;
    font-size: 16px;
    text-align: center;
}
.mg-bound__popover{
    position: absolute !important;
    left: 0;
    top: 0;
    width: 200px !important;
    min-height: 108px !important;
    padding: 8px !important;
    padding-left: 40px !important;
    border-top-right-radius: 10px;
    border-bottom-right-radius: 10px;
    overflow: hidden;
    border-bottom: none !important;
    transform: translateX(-200px);
    -ms-transform: translateX(-200px);
    -webkit-transform: translateX(-200px);
    transition: all .3s;
    background-color: #0066ff;
    text-align: left;
    font-size: 12px;
}
.mg-bound__popover li {
    border-left: 2px solid #000;
    padding: 4px;
}
.mg-bound:hover .mg-bound__popover{
    display: block;
    transform: translateX(0);
    -ms-transform: translateX(0);
    -webkit-transform: translateX(0);
    transition: all .3s;
}

 

版权声明:原创作品,未经允许不得转载,否则将追究法律责任。
本站资源有的自互联网收集整理,如果侵犯了您的合法权益,请联系本站我们会及时删除。
本站资源仅供研究、学习交流之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。
本文链接:子佩工作室https://zptheme.com/6066.html
许可协议:《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权

本站分为普通会员,SVIP会员,永久会员。
SVIP会员新用户注册即送180天,到期后可邀请好友2位/180天进行无限续期。
永久会员支持微信支付在线开通。

积分是本站通用虚拟货币,可用于文章资源购买。
每天签到,评论或点赞文章,或者投稿都可免费获得积分
新用户注册免费赠送2积分 邀请用户注册2位/2积分

如果您已经成功付款但是网站没有弹出成功提示,请联系售后提供付款信息为您处理

本站资源属于虚拟商品,具有可复制性,可传播性,一旦授予,不接受任何形式的退款、换货要求。请您在购买获取之前确认好 是您所需要的资源

需要效果图的,可以联系售后(右侧核保售后,扫码添加或者点击 加入我们-联系客服)。或者微信添加企业微信:子佩工作室

计划书请提供费率条款(现价分红),100起。WP主题一般单个问题30起。主题二开500起,开发新主题3000起。低于1000须全款,其余先支付全款的60%,完成后支付余下的。

资源是指寿险计划书(定制除外),WP主题或插件不包含在永久会员之内!

发表回复

评论(3)