Created
November 28, 2012 01:58
-
-
Save colmdoyle/4158566 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$content = get_the_content(); | |
$nodes = extract_tags($content, 'h\d+', false); | |
$headingsList = ""; | |
$currentHeading = 0; | |
$previousHeading = 0; | |
$level = 0; | |
if($instance['level']) | |
$level = substr($instance['level'], 1, 2); | |
if($level){ | |
foreach ($nodes as $key=>$node) { | |
if (substr($node['tag_name'], 1, 2) > $level) | |
unset($nodes[$key]); | |
} | |
} | |
foreach ($nodes as $node) { | |
$previousHeading = $currentHeading; | |
$currentHeading = substr($node['tag_name'], 1, 2); | |
if ($currentHeading < $previousHeading) { | |
for($i=0 ; $i<$previousHeading-$currentHeading ; $i++){ | |
$headingsList .= '</ul>'; | |
} | |
} | |
if ($currentHeading > $previousHeading) { | |
$headingsList .= '<ul>'; | |
} | |
$headingsList .= '<li><a href="#'; | |
$headingsList .= str_replace(" ", "_", strip_tags($node['contents'])); | |
$headingsList .= '">'; | |
$headingsList .= substr(strip_tags($node['contents']),0,$instance['hchars']); | |
$headingsList .= '</a></li>'; | |
} | |
if (count($nodes) >= 1){ | |
echo $before_widget; | |
if (strlen($instance['title']) > 0) { | |
echo $before_title.$instance['title'].$after_title; | |
} | |
echo $headingsList; | |
echo $after_widget; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment