WordPress

WordPress无插件实现内容目录索引

鉴于 WP 程序的负载很大,能少使用插件就少使用。那有没有不使用插件可以实现这样的索引功能能?于是我找着找着,终于找到一个博主分享的这样的无插件实现内容索引效果,修改模板所在的 function.php 文件。

添加代码:

  1. function article_index($content) {  
  2.     $matches = array();  
  3.     $ul_li = '';  
  4.     $r = "/<h3>([^<]+)<\/h3>/im";  
  5.     if(preg_match_all($r$content$matches)) {  
  6.         foreach($matches[1] as $num => $title) {  
  7.             $content = str_replace($matches[0][$num], '<h4 id="title-'.$num.'">'.$title.'</h4>', $content);  
  8.             $ul_li .= '<li><a href="#title-'.$num.'" title="'.$title.'">'.$title."</a></li>\n";  
  9.         }  
  10.         $content = "\n<div id=\"article-index\"> 
  11.                 <strong>文章目录</strong> 
  12.                 <ul id=\"index-ul\">\n" . $ul_li . "</ul> 
  13.             </div>\n" . $content;  
  14.     }  
  15.     return $content;  
  16. }  
  17. add_filter( "the_content""article_index" );  

优化 CSS:

在我们内容编辑的时候,切换到 HTML 编辑器,然后在需要索引的标题用 H3 标注,具体的用 H 几要和上面的定义 FUNCTION 对应,默认的是 H3。然后在显示的时候会自动抓取。为了达到美化的效果,需要用到 CSS,具体的还是要根据需求调整。

也想出现在这里?联系我们
创客主机
  1. #article-index {  
  2.     -moz-border-radius: 6px 6px 6px 6px;  
  3.     border1px solid #DEDFE1;  
  4.     floatrightright;  
  5.     margin: 0 0 15px 15px;  
  6.     padding: 0 6px;  
  7.     width200px;  
  8.     line-height23px;  
  9. }  
  10. #article-index strong {  
  11.     border-bottom1px dashed #DDDDDD;  
  12.     displayblock;  
  13.     line-height30px;  
  14.     padding: 0 4px;  
  15. }  
  16. #index-ul {  
  17.     margin: 0;  
  18.     padding-bottom10px;  
  19. }  
  20. #index-ul li {  
  21.     backgroundnone repeat scroll 0 0 transparent;  
  22.     list-style-typedisc;  
  23.     padding: 0;  
  24.     margin-left20px;  
  25. }  

收藏
(0)
PetitQ

PetitQ

文章:24464 画廊:208 视频:17 商品:108

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

最新视频

热评视频

发表回复

热销模板

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

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