WordPress教程

为wordpress文章添加目录方便阅读

阿里云

在阅读文章的时候,有个目录来导航,相信还是很方便阅读的,所以本站也弄了这么一块,特别是长点的文章,导航还是很有必要的,在 functions.php 中添加创建目录的函数,代码中的目录标签你要以自行定义,本站用的是 h3 标签:

  1. // 文章内容添加文章目录
  2. function content_index($content) {
  3.   if(is_single()){
  4.     $matches = array();
  5.     $ul_li = '';
  6.     $r = "/<h3>([^<]+)<\/h3>/im";
  7.     $i='1';
  8.     if(preg_match_all($r, $content, $matches)) {
  9.         foreach($matches[1] as $num => $title) {
  10.             $content = str_replace($matches[0][$num], '<h3 id="title-'.$num.'">'.$title.'</h3>', $content);
  11.             $ul_li .= '<li><a class="smooth" href="#title-'.$num.'" title="'.$title.'">'.$i.'.'.$title."</a></li>\n";
  12.             $i ++;
  13.         }
  14.         $content = "\n<div id=\"article-index\"><i class=\"fa fa-angle-double-right\" ></i><h4>目 录</h4>
  15.                 <ul id=\"index-ul\">\n" . $ul_li . "</ul>
  16.             </div>\n" . $content;
  17.     }
  18. }
  19.     return $content;
  20. }
  21.  
  22. add_filter( "the_content", "content_index", 13 );
也想出现在这里?联系我们
创客主机

js 代码

在 main.js 中添加以下代码,当点击目录时,能平滑移动到相应的位置,添加了一个样式:smooth

  1. //锚点滑动:在href上加上一个样式:smooth        
  2. $(".smooth").click(function(){
  3. var href = $(this).attr("href");
  4. var pos = $(href).offset().top-100;
  5. $("HTML,body").animate({scrollTop: pos}, 1000);
  6. return false;
  7. });

添加样式

样式根据自己的网站风格设置就是。

  1. #article-index {
  2. 	position: fixed;
  3. 	right: 66px;
  4. 	bottom:0px;
  5. 	padding: 10px;
  6. 	border-radius: 5px;
  7. 	z-index: 99999999;overflow:hidden;display:none;opacity:0
  8. }
  9. #article-index i {
  10. 	color: #fff;
  11. 	font-size: 20px;
  12. 	margin-right: 10px;
  13. }
  14. #article-index i:hover{cursor:pointer}
  15. #article-index h4 {
  16. 	font-size: 16px;
  17. 	color: #fff;
  18. 	font-weight: 400;
  19. 	margin: 0;
  20. 	text-transform: uppercase;
  21. 	position: relative;
  22.         text-align:center;display:inline-block
  23. }
  24.  
  25. #article-index ul {
  26. 	margin: 10px auto 0;
  27. 	padding: 10px;
  28. 	max-height: calc(100vh - 178px);max-width:200px;
  29. 	overflow-y: auto;overflow-x:hidden;
  30. 	background: #fff;
  31. }
  32. #article-index ul li {
  33. 	list-style: none;
  34. 	margin-bottom: 8px;
  35. 	white-space: nowrap;
  36. 	overflow: hidden;
  37. 	text-overflow: ellipsis;
  38. }
  39. #article-index ul li a{
  40.     font-size: 12px;}

添加目录

本站显示目录是放在右下侧导航上的,以下代码是显示目录与点击目录上隐藏按钮的 JS

  1. //文章目录按钮
  2. $('#article-index i').click(function(){
  3.     $('#article-index').animate({'bottom':'0','opacity':'0'},600,function(){$(this).css('display','none')});
  4. })
  5.  
  6. $('.wz-index').click(function(){
  7.     $('#article-index').css('display','block').animate({'bottom':'56px','opacity':'1'},600);
  8. })

使用目录

要想在文章中能显示目录,在发表文章时,要含有目录标签,如本站的是 h3,那要写文章时,要有 h3 标签的标题。最终显示效果如下:

WordPress 文章添加目录方便阅读

已有 504 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!