Dynamic Content with JavaScript Custom Post Formats Advanced CSS Techniques Performance Optimization Integrating Third-Party Services Creating Theme Options Add this to your theme.xml
:
<div id="dark-mode-toggle">🌓</div>
Add this JavaScript to your script.js
:
document. addEventListener ( 'DOMContentLoaded' , function () {
const toggle = document. getElementById ( 'dark-mode-toggle' );
const body = document.body;
toggle. addEventListener ( 'click' , function () {
body.classList. toggle ( 'dark-mode' );
localStorage. setItem ( 'darkMode' , body.classList. contains ( 'dark-mode' ));
});
if (localStorage. getItem ( 'darkMode' ) === 'true' ) {
body.classList. add ( 'dark-mode' );
}
});
Add these styles to your CSS:
.dark-mode {
--background-color : #1a1a1a ;
--text-color : #f0f0f0 ;
--primary-color : #4a90e2 ;
}
#dark-mode-toggle {
cursor : pointer ;
position : fixed ;
top : 20 px ;
right : 20 px ;
font-size : 24 px ;
}
Add this to your theme.xml
inside the Blog widget:
<b:loop values='data:posts' var='post'>
<b:if cond='data:post.labels any (label => label.name == "Featured")'>
<div class='featured-post'>
<h2><data:post.title/></h2>
<img expr:src='data:post.featuredImage' alt='Featured Image'/>
<p><data:post.snippet/></p>
<a expr:href='data:post.url'>Read More</a>
</div>
<b:else/>
<!-- Regular post format -->
<div class='regular-post'>
<!-- ... -->
</div>
</b:if>
</b:loop>
Style your featured posts:
.featured-post {
background-color : var ( --primary-color );
color : white ;
padding : 20 px ;
margin-bottom : 30 px ;
border-radius : 8 px ;
}
.featured-post img {
width : 100 % ;
height : 300 px ;
object-fit : cover ;
border-radius : 8 px ;
margin-bottom : 15 px ;
}
Update your main content area in theme.xml
:
<main>
<div class='post-grid'>
<b:section id='main' showaddelement='true'>
<b:widget id='Blog1' locked='true' title='Blog Posts' type='Blog'/>
</b:section>
</div>
</main>
Add this CSS:
.post-grid {
display : grid ;
grid-template-columns : repeat ( auto-fill , minmax ( 300 px , 1 fr ));
gap : 20 px ;
}
.post {
background-color : var ( --background-color );
border : 1 px solid var ( --primary-color );
border-radius : 8 px ;
overflow : hidden ;
transition : transform 0.3 s ease ;
}
.post:hover {
transform : translateY ( -5 px );
}
Add this JavaScript to your script.js
:
document. addEventListener ( 'DOMContentLoaded' , function () {
const images = document. querySelectorAll ( 'img[data-src]' );
const config = {
rootMargin: '0px 0px 50px 0px' ,
threshold: 0
};
let observer = new IntersectionObserver ( function ( entries , self ) {
entries. forEach ( entry => {
if (entry.isIntersecting) {
preloadImage (entry.target);
self. unobserve (entry.target);
}
});
}, config);
images. forEach ( image => {
observer. observe (image);
});
});
function preloadImage ( img ) {
const src = img. getAttribute ( 'data-src' );
if ( ! src) { return ; }
img.src = src;
}
Update your image tags in theme.xml
:
<img data-src='data:post.thumbnailUrl' alt='Post Thumbnail'/>
Add this to your theme.xml
where you want comments to appear:
<b:if cond='data:blog.pageType == "item"'>
<div id="disqus_thread"></div>
<script>
var disqus_config = function () {
this.page.url = '<data:blog.canonicalUrl/>';
this.page.identifier = '<data:blog.pageId/>';
};
(function() {
var d = document, s = d.createElement('script');
s.src = 'https://YOUR_SHORTNAME.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
</script>
</b:if>
Replace YOUR_SHORTNAME
with your Disqus shortname.
Add this to your theme.xml
inside the <head>
tag:
<b:skin><![CDATA[
:root {
--primary-color: $(primary.color);
--secondary-color: $(secondary.color);
--text-color: $(text.color);
--background-color: $(background.color);
}
]]></b:skin>
<b:template-skin>
<b:variable default='#3498db' name='primary.color' type='color'/>
<b:variable default='#2ecc71' name='secondary.color' type='color'/>
<b:variable default='#333333' name='text.color' type='color'/>
<b:variable default='#ffffff' name='background.color' type='color'/>
</b:template-skin>
This creates color options that can be changed in the Blogger theme editor.
These advanced techniques will help you create a more dynamic, performant, and customizable Blogger theme. Remember to test thoroughly after implementing these features, especially across different devices and browsers.
In the next section, we'll cover SEO optimization to help your Blogger theme perform well in search engines. Keep experimenting and refining your theme to create the best possible experience for your blog visitors!