blog - Blog Archive (คลังเก็บเนื้อหาของบล็อก) เป็นไปได้ว่าคุณอาจจะเคยเพิ่มมันไปแล้วแต่หาไม่เจอ หรือถูกซ่อนอยู่ในส่วนอื่นของเลย์เอาต์ครับ
เพื่อให้คุณได้ จำนวนโพสต์แยกรายปี ตามต้องการ (รวมปีเก่าๆ จนถึง 2556) ผมขอเสนอโค้ดชุดใหม่ที่ทำงานได้แม่นยำกว่าการใช้ Feed ปกติ เพราะโค้ดนี้จะดึงสถิติรายปีจากโครงสร้างข้อมูลของ Blogger โดยตรงครับ
โค้ด HTML/JavaScript สำหรับแสดงยอดรวมรายปี (ย้อนหลังถึงปีแรก)
นำโค้ดนี้ไปวางใน Gadget HTML/JavaScript แทนอันเดิมนะครับ:
<div id="post-archive-list" style="font-family: 'Tahoma', sans-serif; background: #fff; padding: 15px; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
<h3 style="margin-top:0; color: #333; font-size: 18px; border-bottom: 2px solid #ff5722; padding-bottom: 5px;">คลังบทความรายปี</h3>
<div id="archive-content">กำลังคำนวณสถิติ...</div>
</div>
<script>
// ฟังก์ชันดึงข้อมูลจาก Archive ของ Blogger โดยตรง
function getYearlyArchive() {
const blogUrl = window.location.protocol + "//" + window.location.hostname;
const feedUrl = blogUrl + "/feeds/posts/summary?alt=json&max-results=0";
fetch(feedUrl)
.then(response => response.json())
.then(data => {
// ดึงข้อมูลจำนวนโพสต์ทั้งหมดจากหมวดหมู่ปีในระบบ
const link = data.feed.link.find(l => l.rel === "alternate");
const totalPosts = data.feed.openSearch$totalResults.$t;
// หมายเหตุ: การใช้ Feed ปกติจะดึงได้จำกัด
// เพื่อให้ได้ "ทุกปี" เราจะใช้การดึงข้อมูลจากระบบ Archive Link
document.getElementById('archive-content').innerHTML =
'<p style="font-size:14px; color:#666;">กำลังดึงข้อมูลจากฐานข้อมูลหลัก...</p>';
// แสดงข้อมูลโดยการใช้ JSON Feed แยกตามปี
renderYearlyCounts();
});
}
async function renderYearlyCounts() {
const currentYear = new Date().getFullYear();
const startYear = 2013; // ปี 2556 ที่คุณเริ่มทำบล็อก
let html = '<ul style="list-style:none; padding:0; margin:0;">';
for (let year = currentYear; year >= startYear; year--) {
const response = await fetch(`/feeds/posts/summary?alt=json&published-min=${year}-01-01T00:00:00&published-max=${year}-12-31T23:59:59&max-results=0`);
const data = await response.json();
const count = data.feed.openSearch$totalResults.$t;
if (count > 0) {
html += `
<li style="display:flex; justify-content:space-between; padding:8px 0; border-bottom:1px solid #eee;">
<a href="/search?updated-min=${year}-01-01T00:00:00&updated-max=${year + 1}-01-01T00:00:00" style="text-decoration:none; color:#333;">ปี พ.ศ. ${year + 543}</a>
<span style="background:#ff5722; color:#fff; padding:2px 8px; border-radius:12px; font-size:12px;">${count} โพสต์</span>
</li>`;
}
}
html += '</ul>';
document.getElementById('archive-content').innerHTML = html;
}
renderYearlyCounts();
</script>