Table Of Contents In The Sidebar

Apr 12, 2009   //   by Hackadelic   //   Featured, WordPress  //  19 Comments
This entry is part of a series, TOC Advice»

2609655539 b644e5e1b2 t Table Of Contents In The SidebarHackadelic TOC Boxes work in the sidebar as well. Out of the box. You can see a live demo on my blog by going to a post’s single view: The TOC box appears at the top of the left sidebar.1 This post here is a good example.

How I did it?

The trick is fairly simple: After enabling shortcode in widgets, I simply added a text widget with no title to my sidebar, and entered the shortcode:

[toc style="margin-bottom:1em; margin-right:1em"]

That’s it.

Since a TOC is only generated in “single mode” (meaning when viewing single posts or pages), the widget occupies no space on the front page or archive pages. In singular views, it expands to the TOC of the current post or page (if it has any headers).

Hence, this is an alternative way of “automatically” adding a TOC to all your posts – complementary to the auto-insertion feature. In particular in blogs that use in-post images extensively, adding a TOC in the sidebar is the less obtrusive and problematic way than auto-inserting it into the post, where it might interfere with the individual post layout / typesetting.

  1. At least it’s at the top at time of this writing. I may move it in the future to another place, or remove it all together. []

19 Comments

  • Hello,

    is there a way of inserting it directly in the theme code?

    I have many pages and posts already created and I would like to have some kind of so I can put in my custom-page-theme.php before the_content()… so the TOC will be automatically added to all my pages and posts that I have written so far…

    I havent figured out how to do this with your custom code [toc title="TOC box title" hint="hover hint"
    class="extra CSS class" style="inline CSS style"]…

    Can you help me on this?

    Thanks and regards
    Antonio

    • Antonio, you don’t need to insert it to your theme. Either add it to your sidebar as described here, where it will automatically show up on every post and page (that has headings), or use the auto-insertion option.

  • yes, you right.i do tried on my website to use it on the footer but didn’t worked.i will check it again.i finally used it on the sidebar and it worked perfectly.there is one more issue in my opinion.your article should emphasize the fact that the TOC is built after it’s looking for lines that are “Headline”(1-6 i assume).giving some screen shot example’s will be the best.

  • Well as a matter of fact, i didn’t really thought about it very deep.there is the technical issue which i think \supporting this widget on the footer will make this widget more generic for all of the themes that has 3/4 footer sections.
    in a website design thinking , you are probably right.placing the TOC at the footer does not make any sense.

    • Yoav, I see. And while semantically putting a TOC into a 3/4 footer still doesn’t make sense to me, it is technically already possible – because most 3/4 footer implementations are technically “sidebars” that can contain widgets.

  • Hi There.
    One problem thou, the toc wont work as a widget on a footer.only on a side bar.think of adding this option?

    • Yoav, I don’t see how a TOC in the footer can make sense?!?

  • Thanks, I ended up widgetizing my theme (still in development). For the record I had to:
    1. add the following to my theme’s functions.php:

    if (!is_admin())
    add_filter('toc', 'do_shortcode', 11);

    2. add a text widget to the sidebar containing [toc].

    I was trying to use the PHP Code widget, but it doesn’t work.

  • I use a customised theme that doesn’t use widgets in the sidebar. Is it possible to use this in the sidebar without converting the theme to use widgets? If so, how? Thanks.

  • Actually I tried both. The “wrong” one and your afterwards. Currently it’s yours beeing active. None of them made the TOC appear.

  • ps: maybe your comment-editor would benefit from buttons (for the sloppy or confused users like me that tend to try codes they don’t know xD )

  • Hi there.
    I have a very strange problem: I CAN use your plugin fine if I insert the shortcode into a post or page. However I can’t do the same in my already shortcode-enabled sidebar widget although I’ve exactly followed your steps. Also I’ve double-checked if shortcodes are really enabled and yes, they are. Other plugins can use their shortcodes just fine. (tested with NEXTGen Gallery).
    (The odd bar at the top of the sidebar is my try with your advice.)

  • This plugin rules. Thanks!

  • I couldn’t figure out how to do this at first. To clarify, you must add a Text widget under the Appearance -> Widgets menu, then edit this new widget and enter [toc] in the text field.

    • Perry, exactly.

Leave a comment

Please ignore these 2 fields:

Blog Categories

I have come here to chew bubblegum and kick ass...
and I'm all out of bubblegum.
-- Nada in They Live