给文章添加文章目录功能,不仅是文章条理更清楚,还有利于SEO,下面将介绍 露兜 老大的使用代码来实现文章目录的方法,方便喜欢折腾代码的朋友,如果你不想折腾代码,你可以试试下面的WordPress文章目录插件:TOC 和 Content Index for WordPress。
用代码实现文章目录
1.将下面的代码添加到主题的 functions.php 文件的 最后一个 ?> 前面:
function article_index($content) {
/**
* 名称:文章目录插件
* 作者:露兜
* 博客:http://www.ludou.org/
* 最后修改:2011年2月10日
*/
$matches = array();
$ul_li = '';
$r = "/<h3>([^<]+)</h3>/im";
if(preg_match_all($r, $content, $matches)) {
foreach($matches[1] as $num => $title) {
$content = str_replace($matches[0][$num], '<h3 id="title-'.$num.'">'.$title.'</h3>', $content);
$ul_li .= '<li><a href="#title-'.$num.'" title="'.$title.'">'.$title."</a></li>n";
}
$content = "n<div id="article-index">
<strong>文章目录</strong>
<ul id="index-ul">n" . $ul_li . "</ul>
</div>n" . $content;
}
return $content;
}
add_filter( "the_content", "article_index" ); |
function article_index($content) {
/**
* 名称:文章目录插件
* 作者:露兜
* 博客:http://www.ludou.org/
* 最后修改:2011年2月10日
*/
$matches = array();
$ul_li = '';
$r = "/<h3>([^<]+)</h3>/im";
if(preg_match_all($r, $content, $matches)) {
foreach($matches[1] as $num => $title) {
$content = str_replace($matches[0][$num], '<h3 id="title-'.$num.'">'.$title.'</h3>', $content);
$ul_li .= '<li><a href="#title-'.$num.'" title="'.$title.'">'.$title."</a></li>n";
}
$content = "n<div id="article-index">
<strong>文章目录</strong>
<ul id="index-ul">n" . $ul_li . "</ul>
</div>n" . $content;
}
return $content;
}
add_filter( "the_content", "article_index" );
使用说明
在编辑文章的时候,在可视化模式下,选中文字,设置为标题3(H3),或者切换到HTML模式,将需要添加到目录中的标题用<h3>和</h3>括起来就可以了,如<h3>我是索引标题</h3>
。当然你也可以用其他标签,如<h2>,<p>等,将以上代码第12行中的h3改成你自己的标签名称就可以了。
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;
} |
#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;
}
以上代码的功能比较单一,只有单级目录,不能实现多层级的复杂而完善的索引目录功能,如果你需要这些功能,那你就要借助文章目录插件:TOC 和 Content Index for WordPress。
来源:
https://www.wpdaxue.com/wordpress-content-index-code.html
微信公众号
手机浏览(小程序)
Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(): Failed to enable crypto in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_33468.jpg): failed to open stream: operation failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57