<div id="related-posts"></div>
<script>
// Automatically show related posts
(function() {
const currentPostLabels = [<data:post.labels.map(label => `"${label.name}"`).join(',')>];
const widget = document.getElementById('related-posts');
if(currentPostLabels.length > 0) {
const script = document.createElement('script');
script.src = `/feeds/posts/default/-/${currentPostLabels[0]}?alt=json-in-script&callback=showRelatedPosts`;
document.head.appendChild(script);
}
window.showRelatedPosts = function(data) {
let html = '<h3>You Might Also Like</h3><ul>';
data.feed.entry.slice(0, 3).forEach(post => {
if(post.title.$t !== document.title) {
html += `<li><a href="${post.link.pop().href}">${post.title.$t}</a></li>`;
}
});
html += '</ul>';
widget.innerHTML = html;
};
})();
</script>

No comments:
Post a Comment