本文是《原创(共25篇)》专题的第 13 篇。阅读本文前,您可以先阅读前面的一些文章:
B2主题没有自带目录,有时候浏览文章时翻来翻去很麻烦,用户体验不好,所以自己通过AI写了个
看图:
第一步:
[gzh2v keyword="1437" key="143713"]
在主题模板函数 (functions.php)中添加以下代码:
/*文章内页添加浮动目录,设计趁年华https://sjcnh.cn/*/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');
/*文章内页添加浮动目录,设计趁年华https://sjcnh.cn/*/
第二步:
在style.css中添加样式以下代码
/*文章内页浮动目录 设计趁年华https://sjcnh.cn*/.mg-bound {
height: 108px;
box-shadow: 0 2px 8px rgb(0 0 0 / 10%);
color: #fff;
position: fixed;
left: 6.7%;
top: 500px;
font-size: 14px;
text-align: center;
z-index: 999999;
}
.mg-bound__hover-block{
border: none !important;
position: absolute !important;
left: 0;
z-index: 3;
padding: 0 !important;
width: 40px;
height: 108px;
border-radius: 4px;
color: #fff;
background-color: #00c18e;
}
.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: #0000008a;
text-align: left;
font-size: 12px;
display: none;
}
.mg-bound__popover li {
border-left: 2px solid #ff5d8f;
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://sjcnh.cn*/[/gzh2v]
第三步:
刷新浏览器缓存以查看
您已阅读完《原创(共25篇)》专题的第 13 篇。您可以继续阅读该专题下面的其它文章: