Hello everyone, welcome to net career, a one stop solution for Google AdSense approval.
Below are codes needed to add to display table of content in every post
The code which has to be pasted before </head> of the theme template HTML page
<script>
// Table of Contents Generator
function generateTableOfContents() {
const toc = document.getElementById('table-of-contents');
if (!toc) return;
const postContent = document.querySelector('.post-body');
if (!postContent) {
toc.style.display = 'none';
return;
}
const headings = postContent.querySelectorAll('h2, h3, h4, h5, h6');
if (headings.length === 0) {
toc.style.display = 'none';
return;
}
toc.innerHTML = '<h2>Table of Contents</h2><ul></ul>';
const tocList = toc.querySelector('ul');
headings.forEach((heading) => {
const link = document.createElement('a');
link.textContent = heading.textContent;
link.href = '#' + heading.id;
link.addEventListener('click', (e) => {
e.preventDefault();
heading.scrollIntoView({ behavior: 'smooth' });
});
const listItem = document.createElement('li');
listItem.appendChild(link);
tocList.appendChild(listItem);
});
}
window.addEventListener('DOMContentLoaded', generateTableOfContents);
</script>
The code which has to be pasted in every blog post
<div id="table-of-contents"></div>
Credit: Chat GPT
Comments
Post a Comment
Hello every one welcome to "Net Career" where you get to know about earning money by staying at home by making blogging as career. Please leave your comment, suggestion or any query regarding this article.