All Tutorials

Your One-Stop Destination for Learning and Growth

Creating an Automatic Table of Contents with Categories for Your Blog

Blogging is an effective way to share your thoughts, ideas, and knowledge with the world. However, maintaining a well-organized and easy-to-navigate blog can be a challenge, especially as you add more posts over time. In this post, we'll explore how to create an automatic table of contents with categories for your blog using simple HTML and a plugin for popular blogging platforms like WordPress.

Prerequisites

Before we dive into the implementation details, let's ensure that your blog meets these requirements:

  1. Your blogging platform supports using custom HTML or has a built-in table of contents feature.
  2. Your blog posts have consistent category labels.

Now, let's walk through creating an automatic table of contents for your blog.

Using Simple HTML

If you use a static site generator like Jekyll or Hugo, you can add the following code snippet to the _layouts/post.html file to generate an auto-updated table of contents for each post:

<nav class="site-navigation">
  <ul class="menu">
    {% assign categories = site.posts | group_by:"categories" %}
    {% for category, posts in categories %}
      <li><a href="#{{category}}">{{ category }}</a></li>
      {% for post in posts %}
        <ul>
          <li><a href="{{ post.url }}">{{ post.title | escape }}</a></li>
        </ul>
      {% endfor %}
    {% endfor %}
  </ul>
</nav>

Replace "_post.html" with the name of your blog post template file if it differs from the standard name. This code snippet will create a nested list for each category and its corresponding posts. Adjust the class names as needed.

WordPress Plugin Solution

If you're using WordPress, there are several plugins available that can generate an automatic table of contents for your blog. One popular option is "Table of Contents Plus." Here's how to set it up:

  1. Install and activate the plugin.
  2. Navigate to the post you wish to edit.
  3. In the WordPress editor, switch to the Text tab (or use the block editor and add a custom HTML block).
  4. Add the following shortcode at the location where you want your table of contents to appear: [toc].
  5. Preview or publish the post to see the generated table of contents in action.

The Table of Contents Plus plugin also allows you to customize the depth and style of the generated TOC. You can modify these settings under "Settings > Table of Contents."

Conclusion

Creating an automatic table of contents with categories for your blog not only makes your content more accessible but also enhances the overall user experience. By using simple HTML or WordPress plugins, you can easily add this feature to your blog and enjoy a well-organized, easy-to-navigate site. Happy blogging!

Published May, 2015