WordPress给文章添加文章索引和文章目录免插件

  • A+
所属分类:wordpress建站

给文章添加文章目录功能,不仅是文章条理更清楚,还有利于SEO,下面将介绍使用代码来实现文章目录的方法,方便喜欢折腾代码的朋友,如果你不想折腾代码,你可以试试下面的WordPress文章目录插件:TOC和Content Index for WordPress。

WordPress给文章添加文章索引和文章目录免插件

代码如下:

function article_index($content) {
$matches = array();
$ul_li = '';
$r = "/([^<]+)</h3>/im";
if(preg_match_all($r, $content, $matches)) {
foreach($matches[1] as $num => $title) {
$content = str_replace($matches[0][$num], ''.$title.'', $content);
$ul_li .= ''.$title."n";
}
$content = "n
文章目录
n" . $ul_li . "
n" . $content;
}
return $content;
}
add_filter( "the_content", "article_index" );

述代码添加到主题functions.php文件中最后一个>;之前即可,

然后需要在主题style.css文件中添加如下的css样式

#article-index {
-moz-border-radius: 6px 6px 6px 6px;
border: 1px solid #DEDFE1;
float: right;
margin: 0 0 15px 15px;
padding: 0 6px;
width: 200px;
line-height: 23px;
}
#article-index strong {
border-bottom: 1px dashed #DDDDDD;
display: block;
line-height: 30px;
padding: 0 4px;
}
#index-ul {
margin: 0;
padding-bottom: 10px;
}
#index-ul li {
background: none repeat scroll 0 0 transparent;
list-style-type: disc;
padding: 0;
margin-left: 20px;
}

使用说明:在编辑文章的时候,将索引部分设置为3级标题。

bwg
小九

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: