• Resolved josede

    (@josede)


    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');
    • This topic was modified 4 years, 6 months ago by josede.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Steven

    (@shazahm1hotmailcom)

    Sorry, but that code will cause an infinite loop. ezTOC works by calling the the_content filter … so you calling the the shortcode while within the the_content filter ends up creating an infinite loop and thus the white page with no content.

    Thread Starter josede

    (@josede)

    Thx Steven for your fast reply.

    Its possible a solution?

    Steven

    (@shazahm1hotmailcom)

    Sorry, I am not sure about what you are asking.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Insert toc before 2nd h2’ is closed to new replies.