wordpress是一款强大的博客系统,支持各种DIY,今天我就给大家带来使用纯代码给Wordpress添加文章目录的功能,使看文章更简单!并且支持点击目录标题快速定位到文章页面指定内容点。
一、文章目录代码
把以下代码添加到主题的Functions.php文件中
//国外主机测评-www.veidc.com
//文章目录
function article_index($content) {
$matches = array();
$ul_li = '';
$r = '/(.*?)/is';
if(is_single() && preg_match_all($r, $content, $matches)) {
foreach($matches[1] as $key => $value) {
$title = trim(strip_tags($matches[2][$key]));
$content = str_replace($matches[0][$key], ''.$title.'', $content);
$ul_li .= '
'.$title."\n";}
$content = "\n
文章目录
- \n" . $ul_li . "
}
return $content;
}
add_filter( 'the_content', 'article_index' );
二、CSS样式代码
把以下代码添加到主题的“main.css”文件最底部中
#article-index {
-moz-border-radius: 6px 6px 6px 6px;border: 1pxsolid#DEDFE1;
float:right;margin: 0 0 15px 15px;padding: 0 6px;width: 200px;line-height: 23px;
}
#article-index strong {border-bottom: 1pxdashed#DDDDDD;display:block;line-height: 30px;padding: 0 4px;
}
#index-ul {margin: 0;padding-bottom: 10px;
}
#index-ul li {background:none repeat scroll0 0transparent;list-style-type:disc;padding: 0;margin-left: 20px;
}
三、最终效果
关于 国外主机测评
【声明】:本站宗旨是为方便站长、科研及外贸人员,请勿用于其它用途!站内所有内容及资源,均来自网络。本站自身不提供任何资源的储存及下载,若无意侵犯到您的权利,请及时与我们联系。另外群里不定期放出优质免费资源,欢迎加入!