Insert toc before 2nd h2
-
The code works fine if i use:
$new_content .= '<span>gg</span>';
But when i try:
$new_content .= '[toc]';
I tried as well:
$new_content .= do_shortcode( '[toc]' );
It doesnt work, i get a white page with no content.
function insert_toc ($content) { if ((!is_singular('post')) || (strpos($content, '<h2') === FALSE) ) { return $content; } $ChapterCount = 2; // Enter the chapter number, which you'd like an add in front of $content = explode("<h2", $content); $new_content = ''; for ($i = 0; $i < count($content); $i++) { if ($i == $ChapterCount) { $new_content .= '<span>gg</span>'; } if($i>0) { $new_content.= "<h2".$content[$i]; } else { $new_content.= $content[$i]; } } return $new_content; } add_filter('the_content', 'insert_toc');
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Insert toc before 2nd h2’ is closed to new replies.