实用文章:WordPress 文章生成目录索引
优采云 发布时间: 2022-12-02 08:35实用文章:WordPress 文章生成目录索引
WordPress文章目录索引,方便浏览者阅读,文分享一篇最新的免插件生成WordPress文章目录索引代码,可以自动生成h2-h6段落标题的文章生成目录索引,并可以实现分层水平。
来自 Mockingbird 的文章-
WordPress 后期生成目录索引
来自 Mockingbird 的文章-
部分
最新代码版本
在当前主题功能模板functions.php中添加如下代码:文章来自Robin-
扩大和缩小
来自 Mockingbird 的文章-
// declare a function and pass the $content as an argument
function insert_table_of_contents($content) {
// used to determine the location of the
// table of contents when $fixed_location is set to false
$html_comment = "";
// checks if $html_comment exists in $content
$comment_found = strpos($content, $html_comment) ? true : false;
// set to true to insert the table of contents in a fixed location
// set to false to replace $html_comment with $table_of_contents
$fixed_location = true;
// return the $content if
// $comment_found and $fixed_location are false
if (!$fixed_location && !$comment_found) {
return $content;
}
// 设置排除,默认页面文章不生成目录
// exclude the table of contents from all pages
// other exclusion options include:
// in_category($id)
// has_term($term_name)
// is_single($array)
// is_author($id)
if (is_page()) {
return $content;
}
// regex to match all HTML heading elements 2-6
$regex = "~((.*))~";
// preg_match_all() searches the $content using $regex patterns and
// returns the results to $heading_results[]
//
// $heading_results[0][] contains all matches in full
// $heading_results[1][] contains 'heading title
// $heading_results[4][] contains the title text
preg_match_all($regex, $content, $heading_results);
<p>
" />
// 默认小于3个段落标题不生成目录
// return $content if less than 3 heading exist in the $content
$num_match = count($heading_results[0]);
if($num_match < 3) {
return $content;
}
// declare local variable
$link_list = "";
// loop through $heading_results
for ($i = 0; $i < $num_match; ++ $i) {
// rebuild heading elements to have anchors
$new_heading = $heading_results[1][$i] . " id='$i' " . $heading_results[3][$i];
// find original heading elements that don't have anchors
$old_heading = $heading_results[0][$i];
// search the $content for $old_heading and replace with $new_heading
$content = str_replace($old_heading, $new_heading, $content);
// generate links for each heading element
// each link points to an anchor
$link_list .= "" . $heading_results[4][$i] . "";
}
// opening nav tag
$start_nav = "";
// closing nav tag
$end_nav = "";
// title
$title = "Table of Contents";
// wrap links in '' element
$link_list = "" . $link_list . "";
// piece together the table of contents
$table_of_contents = $start_nav . $title . $link_list . $end_nav;
// if $fixed_location is true and
// $comment_found is false
// insert the table of contents at a fixed location
if($fixed_location && !$comment_found) {
// location of first paragraph
$first_paragraph = strpos($content, '</p>', 0) + 4;
// location of second paragraph
$second_paragraph = strpos($content, '</p>', $first_p_pos);
// insert $table_of_contents after $second_paragraph
return substr_replace($content, $table_of_contents, $second_paragraph + 4 , 0);
}
// if $fixed_location is false and
// $comment_found is true
else {
// replace $html_comment with the $table_of_contents
return str_replace($html_comment, $table_of_contents, $content);
}
}
// pass the function to the content add_filter hook
add_filter('the_content', 'insert_table_of_contents');</p>
来自 Mockingbird 的文章-
默认情况下,如果页面和文章中的段落标题少于三个,则不会生成目录。代码中添加中文注释,可酌情修改。来自 Mockingbird 的文章-
部分
层级展示
层级关系可以通过调整样式来展示。文章来自罗宾-
例如:H3为一级,H4、H5、H6为二级
.heading-level-4,
.heading-level-5,
.heading-level-6 {
margin-left: 40px;
}
效果参考文字目录。来自 Mockingbird 的文章-
部分
jQuery平滑滚动代码
需要加载 jQuery.js 来自 Robin 的文章 -
jQuery(document).ready(function($){
$('.table-of-content a[href*=#]').click(function() {
if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
var $target = $(this.hash);
$target = $target.length && $target || $('[name=' + this.hash.slice(1) + ']');
if ($target.length) {
var targetOffset = $target.offset().top;
$('html,body').animate({
scrollTop: targetOffset - 100
},
800);
return false;
}
}
});
})
其中:scrollTop:targetOffset - 100,也就是上面的100px,用于固定导航菜单。来自 Mockingbird 的文章-
如果要显示当前目录的位置,可以考虑使用js-toc,使用JQ提取目录,可以实时显示当前滑动位置所在的目录,但无法实现分层显示。来自 Mockingbird 的文章-
代码来源
来自 Mockingbird 的文章-
部分
目录生成插件
代码版适合有一定动手能力的用户。如果你想省力并且有更多的设置选项,你可以使用下面推荐的一堆目录生成插件。文章来自Robin-使用更多目录插件Easy Table of Contents 文章来自Robin-
您可以使用短代码在 Robin 的文章文章中的任何位置添加目录 - 目录加上 Robin 的文章 -
有一个目录索引小部件可以添加到侧边栏小部件。来自 Mockingbird 的文章-
以上两个插件的后台功能设置非常相似,都有完整的中文语言包。来自 Mockingbird 的文章-
部分
其他目录生成插件
WP后台→插件→安装插件,搜索安装。来自 Mockingbird 的文章-
本文阅读提示:文章采集
伪原创工具、伪原创文章素材、如何伪原创文章
如何采集
网站伪原创文章,一:伪原创文章的采集
方法
本站采集
伪原创文章。我个人建议你用“很多算法和人工智能”来处理它们。这是一个相对较新的搜索引擎,但它是一个同义词。伪原创的方法与伪原创的具体方法类似。请记住,搜索引擎不是傻子,因此我们必须为伪原创修改我们的标题。
网站伪原创文章合集,二:伪原创方法
很多网站都是从网上复制过来的,所以搜索引擎在伪原创方面没有什么特别之处,所以我们只能做一些改动,但是我们这篇文章的伪原创方法是公认的最好的行得通,所以我只能做一些修改放在最后。
网站伪原创文章合集,三:伪原创方法
很多网站都有大量的文章,我们可以根据这些文章得出一些不同的观点,从而获得不同的原创效果。这就是我们所说的伪原创方法。
" />
伪原创网站文章合集,一:伪原创方法
很多网站都是通过伪原创的方式获取的,搜索引擎并不需要我们使用很多伪原创工具,因为我们使用少量的文章会被搜索引擎惩罚,所以我们需要提高质量网站上的伪原创文章。
网站模仿者采集
伪原创文章,二:伪原创的方法
很多网站都是通过伪原创的方式获得的。搜索引擎不需要你使用很多伪原创工具,因为搜索引擎需要的文章比较多,可以在短时间内为我们提供大量的文章。如果使用伪原创方法,还是可以直接使用的,这就是伪原创方法
网站模仿者采集
伪原创文章,三:伪原创方法
很多网站都是通过伪原创的方式获取的,比如:
文章收录法,将一篇或多篇相关文章分成若干篇文章,放入自己的文章中。
网站模仿者采集
伪原创文章,
网站模仿者采集
伪原创文章,
" />
网站模仿者采集
伪原创文章,
网站模仿者采集
伪原创文章,
网站模仿者采集
伪原创文章,
网站模仿者采集
伪原创文章,
网站模仿者采集
伪原创文章,
网站模仿者采集
伪原创文章,
网站模仿者采集
伪原创文章,
seo新手seo需要更好的准备:伪原创文章的网站编辑
相关文章