public
Created

  • Download Gist
gistfile1.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
<?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;
}
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.