-
AuthorPosts
-
May 16, 2023 at 11:32 pm #1407672
Hello,
We need a table of content for long blog pages. So when the users click on a subtitle they are taken directly to that part of the article.
How can we achieve that?May 17, 2023 at 5:42 am #1407700Hi menainfosec,
There’s a widget called Table of Contents which you can use in Appearance > Widgets.
If you have a sidebar for your blog pages, then you should be able to add it there.
Hope it helps.Best regards,
NikkoMay 17, 2023 at 5:39 pm #1407767Thanks for the response. But we want to add it in the body. It doesn’t look good on sidebar.
May 18, 2023 at 1:17 am #1407784Hi,
If your blog posts are created with the Advanced Layout Builder then you can add the Widget Area element to your page and select your widget area, or a custom widget area with the TOC widget.
If you are using the default editor for your posts then you can add this script to your child theme functions.php:function custom_toc_script() { ?> <script> document.addEventListener('DOMContentLoaded', function() { htmlTableOfContents(); } ); function htmlTableOfContents( documentRef ) { var documentRef = documentRef || document; var toc = documentRef.getElementById("toc"); var content = document.querySelector(".single-post .entry-content"); var headings = content ? [].slice.call(content.querySelectorAll( "h1, h2, h3, h4, h5, h6")) : [] headings.forEach(function (heading, index) { var ref = "toc" + index; if ( heading.hasAttribute( "id" ) ) ref = heading.getAttribute( "id" ); else heading.setAttribute( "id", ref ); var link = documentRef.createElement( "a" ); link.setAttribute( "href", "#"+ ref ); link.textContent = heading.textContent; var div = documentRef.createElement( "div" ); div.setAttribute( "class", heading.tagName.toLowerCase() ); div.appendChild( link ); toc.appendChild( div ); }); } try { module.exports = htmlTableOfContents; } catch (e) { // module.exports is not defined } </script> <?php } add_action('wp_footer', 'custom_toc_script');
Then you need to add the TOC div to your post with this:
<div id="toc"></div>
If you are using the RankMath plugin and the Block Editor it will also add a TOC, otherwise there are many plugins that will offer more TOC options.Best regards,
MikeMay 19, 2023 at 12:37 am #1407881ok. thanks. I used a plugin for this
May 19, 2023 at 12:44 am #1407883Hi,
Glad to hear that you have this sorted out, if you have any further questions please create a new thread and we will gladly try to help you. Thank you for using Enfold.Best regards,
Mike -
AuthorPosts
- The topic ‘Table of content for blogs’ is closed to new replies.