Support » Plugin: Table of Contents Plus » [Plugin: Table of Contents Plus] Using a shortcode with TOC

  • Resolved computerslayer1


    I use a custom shortcode in a page. This shortcode displays an h2 tag, followed by paragraphs of text.

    If TOC+ is enabled for pages, it sets the h2’s span ID to all the text in the custom shortcode that follows the first h2 tag. Additionally, the #toc_list element’s anchors display all the text that follows the h2 tag in the custom shortcode as well.

    As you can imagine, it looks like quite a mess.

    Is there a way to fix this?

    Thanks 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Mike


    Thanks for raising that.

    I would suggest to check if the custom shortcode closes the h2 tag properly after the actual heading. TOC+ reads the text between the heading tags so make sure they are closed properly.

    If the above doesn’t work, add a newline (eg echo “\n”) at the end of your headings as TOC+ shouldn’t match anything over multiple lines.

    Yep, properly formed HTML. Here’s an example of the output:

    <h2>My Heading</h2><div class="grid"><div><img src="" width="90" height="113"><p><a href="#">Some text 2</a></p><p>Even more text 2</p><p>987654321</p></div><div><img src="" width="90" height="113"><p><a href="#">Some text</a></p><p>Even more text</p><p>12345678</p></div></div>

    Adding a new line character didn’t seem to have an effect.

    Here’s what I get for output with the TOC+ plugin:

    The HTML that appears in the h2 tag:
    <h2><span id="My_Heading_____Some_text_2___Even_more_text_2___987654321_____Some_text___Even_more_text___12345678">My Heading</span></h2>

    The HTML that appears in the #toc_list:

    <ul class="toc_list">
    <li><a href="#My_Heading_____Some_text_2___Even_more_text_2___987654321_____Some_text___Even_more_text___12345678">My_Heading_____Some_text_2___Even_more_text_2___987654321_____Some_text___Even_more_text___12345678</a></li>

    Plugin Author Mike


    That’s interesting. Are you able to add a newline after the closing heading tag to see if it does the same thing?

    Next up, I’m most likely going to ask for the custom php stuff sitting behind the shortcode so I can try on one of my machines. Is it another plugin you wrote or did you do it via functions.php?

    My mistake–I forgot to copy the plugin file from my IDE to the website after I made the change. So, yes, adding a new line after the closing header element seems to work.

    Plugin Author Mike


    Awesome, thanks for reporting back.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘[Plugin: Table of Contents Plus] Using a shortcode with TOC’ is closed to new replies.