怎么用代码给网站中的文章添加内容目录(WordPress文章目录插件)

小初seo 网站建设评论1,105字数 3147阅读10分29秒阅读模式

去过百度百科的人可能都会注意到,几乎每篇文章的开头都会有一个目录,点击这个目录中的标题可以快速到达文章中的具体内容位置,如:露兜。这样可以方便读者在篇幅较长的文章中找到他们想看的内容,这个也就相当于词典中的索引功能了。

本文所介绍的插件实现的就是这样的一个功能,为文章设置了一个清晰的内容导航,读者可以在阅读之前知道这篇文章的大概意思,点击可以到达他们想看的部分,而且可以增加些内链、锚文本和关键词,对SEO也是很有帮助的。

文章目录免插件代码

其实现这样的一个功能还是比较简单的,也就是在文章内容中插进标题标签,然后弄成目录就是了,下面是我写的一个简单的代码,用文本编辑器打开当前主题目录下的functions.php,将以下代码放到 <?php下面就可以(记得用UTF-8编码保存,否则中文会乱码):

function article_index($content) {
/**
* 名称:文章目录插件
* 作者:露兜
* 博客:https://www.pkak.cn
* 最后修改:2015年3月20日
*/
$matches = array();
$ul_li = '';
$r = "/<h3>([^<]+)<\/h3>/im";
if(is_singular() && preg_match_all($r, $content, $matches)) {
foreach($matches[1] as $num => $title) {
$title = trim(strip_tags($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' );

 

使用说明

在编辑文章的时候,切换到HTML模式,将需要添加到目录中的标题用<h3></h3>括起来就可以了,如<h3>我是索引标题</h3>。当然你也可以用其他标签,如<h1><p>等,将以上代码第12和17行中的h3改成你自己的标签名称就可以了。

上面这段代码只是在文章显示的时候插入文章目录,并不会修改你的文章内容。以上代码也不包括样式美化代码,所以只添加以上代码,文章目录看起来一片混乱,所以你得自己添加一些css代码来美化一下这个目录。如果你不会css,可以用我写的,将以下css代码放到主题目录下的style.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;
}

 

 

支持多级目录

以上代码的功能比较单一,只有单级目录,不能实现多层级的复杂而完善的索引目录功能。网友12READS对以上代码做了修改,据称可以支持多级目录,暂未测试,需要的可以看看

文章目录插件

如果你需要这些功能可以试试这以下这几个插件,使用也都比较简单:(以下插件长时间未更新)

-- 完 --

网友 12READS在评论中给的代码:

改了下代码,现在可以显示下一级了,有需要的请拿走哦:

function article_index($content) {
$matches = array();
$ul_li = '';
$r = '/<h([2-6]).*?\>(.*?)<\/h[2-6]>/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], '<h' . $value . ' id="title-' . $key . '">'.$title.'</h2>', $content);
$ul_li .= '<li><a href="#title-'.$key.'" title="'.$title.'">'.$title."</a></li>\n";
}
$content = "\n<div id=\"article-index\">
<strong>文章目录</strong>
<ol id=\"index-ul\">\n" . $ul_li . "</ol>
</div>\n" . $content;
}
return $content;
}
add_filter( 'the_content', 'article_index' );

 

 

测试有效(记得加CSS)

推荐代码

文中露兜给的代码我用是报错了,我用评论中给的代码替换了一部分代码:

//文章目录:https://www.pkak.cn/
function article_index($content) {
$matches = array();
$ul_li = '';
$r = "/<h3>([^<]+)<\/h3>/im";
if(is_singular() && preg_match_all($r, $content, $matches)) {
foreach($matches[1] as $num => $title) {
$title = trim(strip_tags($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>
<ol id=\"index-ul\">\n" . $ul_li . "</ol>
</div>\n" . $content;
}
return $content;
}
add_filter( 'the_content', 'article_index' );

如何使用

将你需要的标题用H3代码包裹,即可自动在文章页顶部出现目录结构。

 最后更新:2021-6-4
  • 本文由 小初seo 发表于 2021年5月13日11:41:45
  • 转载请务必保留本文链接:https://www.pkak.cn/web/3967.html
匿名

发表评论

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

拖动滑块以完成验证