Suppose you have written a long post article on your blog, covering vast subtopics - then it might become very difficult for anyone to scroll down to find any particular topic. Especially if there came through search engines they might be in hurry for exact one thing they're looking and you're offering them whole lot of text they don't care about.
Here 'Table of Contents' come handily. Table of Contents index the main subtopics and make anyone to navigate easily in your blog post.
If you're on WordPress, there are many plugins available for that. But in case of Blogger (or BlogSpot whatever you call it) you need to look out for external widgets.
You must have tried many tutorials before this, if those didn't worked for you, then this one is very simple, easily to implement and will work for sure!
Implementing Table Of Contents To Blogger
Follow the steps below to implement TOC to Blogger -
- Sign in with Blogger and go to 'Theme'.
- Click on the arrow at 'customize' and select Edit HTML.
- Find <data:post.body/> and place the following code after it.(To find you can use Ctrl+F)
<!--TOC By CSS-MAGZ--> <b:if cond='data:blog.pageType == "item"'> <!-- all item pages --> <script>/*<![CDATA[ */ var el = document.createElement("ol"); el.id = "m"; var z = 0 ; //insert Table of Contents after how many paragraphs var targetElem = "h2"; //Deafult element to target function insertAfter(referenceNode, newNode) { referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling); } var postBody = document.getElementById("Blog1"); var fPara = postBody.getElementsByTagName("p"); var el1 = document.createElement("div"); el1.innerHTML="<button class='toggle' onclick='listToggle();'>Table Of Contents (Show/Hide)</button>"; try{insertAfter(fPara[z], el1);}catch(e){ console.log("CSSMAGZ_TableOfContents: Document Doesn't Contain paragraphs!Trying for div"); insertAfter(postBody.getElementsByTagName("div")[z], el1); } var Elements = postBody.getElementsByTagName(targetElem); for(i = 0; i < Elements.length; i++) { Elements[i].setAttribute("id", "heading"+i); el.innerHTML += "<li class='tgl'><a class='clink' href='#heading"+i+"'>"+Elements[i].textContent+"</a></li>"; insertAfter(el1, el); }; "undefined"!=typeof document&&function(e,t){var n=e.createElement("style");if(e.getElementsByTagName("head")[0].appendChild(n),n.styleSheet)n.styleSheet.disabled||(n.styleSheet.cssText=t);else try{n.innerHTML=t}catch(e){n.innerText=t}} (document,"div .toggle{padding:.5em;background:#696969;border:none;color:#fff;font-weight:700;margin:.3em;}div .toggle:hover{border:.1px solid #000}#m{font-family:sans-serif;margin:0;margin-bottom:10px;background:#faebd7;padding:.3em;border:1px solid #faebd7;animation-name:fade;animation-duration:.3s}@keyframes fade{from{opacity:0}to{opacity:1}}.clink{text-decoration:none;color:##2c44ca}.clink:hover{text-decoration:underline;}div ol li,div ul li{padding:15px 0 0;margin:0 0 0 30px}"); function listToggle() { var tgl = document.getElementById('m'); if (tgl.style.display === 'none') {tgl.style.display = 'block';} else {tgl.style.display = 'none';}} /*]]> */ </script> </b:if>
- Click on save to save the HTML.
- Open some 2-3 existing posts on your blog to make sure TOC properly added to your blog.
Note: If you have multiple <data:post.body/> tags, put the above code after all the tags.
The only easy and best way to add toc is your.
ReplyDeleteit is a good and easy task. But if control the appearance to toc in my post than what can do. Such as in some articals toc appear in after 1st paragraph. Which is the real place but in many articals toc appear after heading1 and not below the paragraph.