Creating an Interactive Table of Contents with Gutenberg

Add a table of contents to your posts to help readers navigate easily, without weighing down your WordPress or server with unnecessary plugins.

If you’ve read my tips on creating engaging blog posts, you know there are a few ways to help readers easily find the sections they’re interested in or quickly get an idea of what the post is about. One way is using a sticky navigation bar or floating menu.

Another option is an interactive table of contents, which also acts as a navigation tool. When readers click on the topics listed in the table of contents, the page automatically scrolls to that section.

Today, I’m going to show you how to create something like that on your blog posts using just Gutenberg blocks. And that’s not all! I’ll also teach you how to make the table of contents easily accessible to readers with just one click. Let’s get started!

How to Build a TOC in Gutenberg

There are plenty of ways to create a table of contents with the basic blocks in Gutenberg. To keep things simple, I’ll walk you through the basics and share a cool feature that lets readers jump back to the table of contents, no matter where they are in the article.

Here’s the good news: we don’t need to code to create a table of contents with Gutenberg. However, it’s helpful to have a basic understanding of HTML IDs and Fragment Identifiers.

Gutenberg has a Table block that helps create the structure for your table of contents. Just insert the table and add details like a brief summary of each section or topic covered in the post. You can adjust the number of rows and columns based on your content or how you want to organize the topics.

It’s important that each line or paragraph in the table matches a heading or section in the blog post. Later, we’ll turn those lines into links. When readers click on them, the page will scroll to the right section, making it easier for them to navigate the post.

The next step is to give HTML ids to the sections of your blog post that you want readers to easily find when they click on items in the table.

These sections can be anything in the editor, like headings, images, or even just a paragraph block. However, the most common blocks to assign HTML ids to are the headings.

To assign an HTML id, select a block and open its settings. Scroll down to the Advanced section and click on it to expand. You’ll see a box called “HTML Anchor.” Just type the HTML id you want to give to that block.

For example, if you have several H2 headings in your blog post, you can give each one a unique HTML id. For the first heading, you might use “first-topic,” for the second one, try “second-topic,” and for the third, you could use “third-subtopic,” and so on. Just remember, each HTML id in your blog post should be unique.

In my case, when I assign an HTML id to my headings, I simply copy the text from the heading and paste it into the HTML Anchor text box. This way, I don’t have to think about creating a custom HTML id for each heading.

Once you paste the heading text into the HTML Anchor box, it will automatically format correctly and be ready to use as a fragment identifier. For example, if your heading says “This is the First Topic,” the HTML Anchor text box will show “This-is-the-First-Topic.”

After assigning HTML ids, we’re ready to turn the text inside our table into links that, when clicked, will take us to different sections of the blog posts.

To do this, highlight the text in the table you want to turn into a link. Then, click the link icon on the toolbar. A text box will pop up where you can paste the matching HTML id as a fragment identifier, starting with a ‘#’. For example, “#This-is-the-First-Topic”. Press enter, and the text will turn into a link with the fragment identifier.

Keep doing this until every line in the table is linked to its corresponding section in the blog post. After that, the navigation feature of your table of contents should be ready to go.

It’s a good idea to test each link to make sure they scroll to the right section when clicked. If everything works, you can move on to the next step.

To check if the “back to table of contents” feature is working, scroll down and see if the ‘back to TOC’ button stays at the top of the screen. When you click it, the table of contents should reappear.

Also, make sure there isn’t a sticky header, as it might cause issues and prevent the feature from working properly.

Once the functionality is working, make sure to style your table of contents and the sticky ‘back to TOC’ button to match your design. Adjust the colors to fit your brand, and consider making the ‘back to TOC’ button smaller so it doesn’t take up too much space in the reading area.

Scroll to Top