Skip to content

@colmdoyle /gist:4158566
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
<?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
Something went wrong with that request. Please try again.