[不花钱站长]:从免费域名开始[0元] Oracle永久免费VPS[0元] VPS环境搭建免费脚本[0元] 秒变大盘挂载免费网盘[0元] 小白免费采集器1天500W[0元] CF免费DNS+CDN服务[0元]
[零基础网赚]:撸本站免费源码[0元] 网站强引免费蜘蛛法[0元] 图片视频文件免费存储[0元] 外链轻松发布免费友链[0元] 入坑免费教程学习源代码[0元] 网上百种+赚钱联盟推荐[?元]

↑↑VPS推荐网 WWW.VPS.COM.IN 稳定 便宜 免备案↑↑


纯代码为WordPress文章内容添加锚点目录功能 支持多级目录

全球[VPS测评]来源:VPS推荐网点击:508
【性价之王】【线路之王】【价格之王】【配置之王】
【免费之王】【香港首推】【梯子之王】【独服之王】
纯代码为WordPress文章内容添加锚点目录功能 支持多级目录

WordPress是功能强大的博客程序,蜗牛789使用的就是WordPress。在平常发布的文章中,有些文章由于篇幅及内容比较多,导致文章有很长的页面,给读者带来不好的体验,那是不是通过设置锚点来为文章做个目录引导呢?当然可以实现,蜗牛说过很多次WordPress只要你想得到的功能基本都有插件,但今天蜗牛为大家介绍的并不是使用插件,只需添加两段代码就能实现。

一、锚点目录代码

我们在主题Functions.php文件中加入下面代码。

//蜗牛789分享-www.wn789.com//文章目录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 h=\"#title-\'.$key.\'\" 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\' );二、CSS样式代码

然后再在主题的“main.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;}三、效果展示



猜你可能想看的VPS


转载请注明原文地址:http://140.238.13.167:12355/read-150507.html

下一篇       上一篇