WordPress文章怎么快速方便的生成目录索引?WordPress文章目录索引怎么生成?

本站:VPS参考评测推荐,专注分享VPS服务器优惠信息!若您是商家也可以在本站进行投稿,查看详情!此外我们还提供软文收录、PayPal代付、广告赞助等服务,查看详情!
部分文章发布时间较久远,可能存在未知因素,购买时建议在本站搜索商家名称,可查看相关文章充分了解商家!若非中文页面可使用谷歌浏览器同步翻译!PayPal代付/收录合作

文章索引相当于文章目录。点击目录自动跳转到对应位置,这就需要你的文章有字幕,那么WordPress文章是如何生成目录索引的呢?我们要做的就是自动识别h3标签并组装成一个目录,好了,开始实现方法吧。

一、实现方法

文章中必须有一个h3标签,每个三级标题都会成为索引中的一个条目。将以下代码放入function.php中,您的文章中会自动生成一个索引。

  • function article_nav($content)
  • {
  •     $matches = array();
  •     $ul_li = ”;
  •     $r = “/

    (.*?)/im”;

  •     if (preg_match_all($r, $content, $matches)) {
  •         foreach ($matches[1] as $num => $title) {
  •             $content = str_replace($matches[0][$num], ‘’ . $title . ‘’, $content);
  •             $ul_li .= ‘
  • ’ . $title . “
  • ”;
  •         }
  •         if (is_singular()) {
  •             $content = ‘Article Nav
  •                 . $ul_li . ‘
  • 发表评论
  • ’ . $content;
  •         }
  •     }
  •     return $content;
  • }
  • add_filter(“the_content”, “article_nav”);
  •  

    二、索引样式

    生成了索引,但只是文章中的一个普通的ul列表。我们需要做的就是把它分开,所以我们只需要为它写一个css样式就可以了。建议使用 position:fixed 将其固定在左侧或右侧。下面提供了一个参考样式,可以根据自己的情况进行修改。

  • #article_nav_title{font-size:11px;text-align:center;line-height:15px;color:#cc0000;border-left:3px #cc0000 solid;border-bottom:1px #aaa dotted}
  • #fn_article_nav{position:fixed;left:5px;top:140px;background-color:rgba(255,255,255,.55);border-radius: 0 3px 3px 0;box-shadow:0 0 2px #aaa}
  • #fn_article_nav ul{padding:0!important;margin:0px!important}
  • #fn_article_nav li{list-style:none;padding:3px;width:100px;margin:0;background: url(“images/li.png”) no-repeat scroll 0 5px transparent!important;text-indent: 1.6em;}
  • 本文由本站刊发,转载请注明:WordPress文章怎么快速方便的生成目录索引?WordPress文章目录索引怎么生成?,https://本站.com/68897.html

    推荐站内搜索:域名解析查询、本机ip查询、服务器租用美国高防、香港服务器、最新域名查询、免费美国主机、腾讯云服务器12元一年、最好的美国服务器、域名注册查询、个人虚拟主机、