我在下面有一个目录类,可以找到所有<h2>
<h3>
和<h4>
在wordpress帖子中添加标签,它将从中创建一个目录,并为每个标题添加一个ID,因此当您单击链接时,它会将您带到页面的该部分。
下面是生成的TOC的快照。。。
现在这个类的问题是,它只需查找所有标题并在页面加载时构建TOC,然后将生成的TOC前置到$content
邮递
我想找出一种方法,我可以指定TOC将在帖子中显示的位置。例如,如果一篇文章有一个特色图片,那么我想在图片下方显示TOC,而不是在文章的开头。有点像shortcode
这将是理想的,但我不知道如何让它与一个短代码一起工作,我尝试创建一个可以运行这个类的短代码,但它并没有像我希望的那样工作,当我使用一个短代码运行它时,它不会创建TOC。
有没有人有什么想法可以改进它,让它像我描述的那样工作?
使用此筛选器运行该类
add_filter(\'the_content\', array(\'TableOfContents\', \'writeTOC\'));
班级
/**
* cd-table-of-contents.php
*/
class TableOfContents {
/**
* Counts the occurence of header elements in Wordpress content
*
* @param type $content
* @return null|boolean|array
*/
static function hasToc($tiers, $content) {
$pattern = \'/<h[2-\' . $tiers . \']*[^>]*>(.*?)<\\/h([2-\' . $tiers . \'])>/\';
$return = array();
if (empty($content))
return null;
if (!preg_match_all($pattern, $content, $return)) {
return false;
}
return $return;
}
/**
* Generates a table of content only when singular pages are being viewed
*
* @param type $tiers
* @param type $text
*/
static function generateTableOfContents($tiers, $content, $draw = TRUE, $return = array()) {
if (!is_singular())
return $content;
// numbers on or off?
$num_on = true;
$content = $toc . $content;
$searches = array();
$replaces = array();
$return = (is_array($return) && !empty($return) ) ? $return : TableOfContents::hasToc($tiers, $content);
if ($draw && !empty($return)):
if($num_on){
$toc = \'<div class="toc nonumbers">\';
}else{
$toc = \'<div class="toc">\';
}
$toc .= "<h4>Table of Contents</h4>";
$toc .= "<ul class=\\"parent start\\">";
$tags = reset($return);
$titles = $return[1];
$levels = end($return);
$_level = 2;
$chapters = array(\'0\',\'0\',\'0\',\'0\',\'0\',\'0\');
$count = 0;
foreach ($tags as $i => $htag) {
$count++;
$attributes = array();
$href = $count;
$newId = \'id="\' . $href . \'"\';
$newhtag = \'><span style="position: absolute; margin-top: -60px;" \' .$newId. \'></span>\';
$htagr = str_replace(\'>\' . $titles[$i], "\\t" . $newhtag . $titles[$i], $htag);
$searches[] = $htag;
$replaces[] = $htagr;
if ((int)$levels[$i] === (int)$_level):
if($num_on){
$chapters[$_level-1] = ((int)$chapters[$_level-1]+1);
$chapter = implode(\'.\', array_slice($chapters, 1, ($levels[$i]-1) ) );
$toc .= \'<li><span>\' . strval($chapter) . \'</span> <a href="#\' . $href . \'">\' . $titles[$i] . \'</a></li>\';
}else{
$toc .= \'<li><a href="#\' . $href . \'">\' . $titles[$i] . \'</a></li>\';
}
endif;
if ($levels[$i] > $_level) {
$_steps = ((int) $levels[$i] - (int) $_level);
for ($j = 0; $j < $_steps; $j++):
$toc .= \'<ol class="continue">\';
$chapters[$levels[$i]-1+$j] = (int)$chapters[$levels[$i]-1+$j]+1;
$_level++;
endfor;
$chapter = implode(\'.\', array_slice($chapters, 1, ($levels[$i]-1) ) );
if($num_on){
$toc .= \'<li><span>\' . strval($chapter) . \'</span> <a href="#\' . $href . \'">\' . $titles[$i] . \'</a></li>\';
}else{
$toc .= \'<li><a href="#\' . $href . \'">\' . $titles[$i] . \'</a></li>\';
}
}
if ($levels[$i] < $_level) {
$_steps = ((int) $_level - (int) $levels[$i]);
$chapters[$levels[$i]-1] = (int)$chapters[$levels[$i]-1]+1;
$_olevel = $_level;
for ($j = 0; $j < $_steps; $j++):
$chapters[$levels[$i]+$j] = 0;
$toc .= \'</ol>\';
$_level--;
endfor;
$chapters[$_olevel-1] = 0;
$chapter = implode(\'.\', array_slice($chapters, 1, ($levels[$i]-1) ) );
if($num_on){
$toc .= \'<li><span>\' . strval($chapter) . \'</span> <a href="#\' . $href . \'">\' . $titles[$i] . \'</a></li>\';
}else{
$toc .= \'<li><a href="#\' . $href . \'">\' . $titles[$i] . \'</a></li>\';
}
}
}
$toc .= \'</ul>\';
$toc .= \'</div><div class="clear"></div>\';
$content = str_replace($searches, $replaces, $content);
$content = $toc . $content;
endif;
return $content;
}
/**
* Appends the table of content to the $content
* AKA. Executes our filter
*
* @param type $content
* @return type
*/
static function writeToc($content) {
$content = TableOfContents::generateTableOfContents(4, $content, TRUE);
return $content;
}
}
add_filter(\'the_content\', array(\'TableOfContents\', \'writeTOC\'));