
添加文章目录的位置在【后台】-【文章】-【分类目录】,分类目录可以在菜单和文章中被使用举例,增加一个名字为“资讯”的目录,然后写文章时选择分类目录为“资讯”,点击“资讯”这个菜单会显示所有归属为“资讯”分类的文章。
欢迎查看我的简介,分享零基础wordpress建站教程。
2种方法实现:1、插件。如Content Index for WordPress
2、像高时银博客那亲通过代码来实现。在主题的functions.php文件中添加如下代码:
function article_index($content) {
$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" )
然后在发表文章时选择要做目录的词语,并用<h3>把它们括起来,如<h3>我是索引标题</h3>。
再就是添加CSS样式就可以了。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)