From 4213194a62843714bd5348101a4a3a811d8df29d Mon Sep 17 00:00:00 2001 From: Dinath Date: Sat, 18 Apr 2020 12:01:58 +0200 Subject: [PATCH] :bug: Fix levels issues --- inc/table-of-contents.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/inc/table-of-contents.php b/inc/table-of-contents.php index ea075b0..d175ef0 100644 --- a/inc/table-of-contents.php +++ b/inc/table-of-contents.php @@ -126,18 +126,25 @@ function get_toc() { continue; } - if ( $level > $level_previous ) { // the previous title is higher h2 > h3. + $levels_to_close = $level_previous - $level; + + if ( $level > $level_previous ) { + // the previous title is higher h2 > h3. $html .= "\n
  1. $link"; - $levels_to_close ++; - } elseif ( $level_previous > $level ) { // the previous title is lower h4 > h2. + } elseif ( $level_previous > $level ) { + // the previous title is lower h4 > h2. + + // close all opened li before closing the list. for ( $l = 0; $l < $levels_to_close; $l++ ) { $html .= '
'; } - $html .= "\n
  • $link"; - $levels_to_close = 0; - } else { // the previous title is the same h2 = h2. + + $html .= "
  • \n
  • $link"; + } else { + // the previous title is the same h2 = h2. $html .= "
  • \n
  • $link"; } + $level_previous = $level; }