
Liputan Khusus
-- --- ----
•
Redaksi Newsroom
Menghubungkan otomatis dengan REST API WordPress KamparMedia...
Memuat 4 artikel Berita Terkini...
document.addEventListener("DOMContentLoaded", function () {
const DOMAIN = "https://kamparmedia.com";
const DEFAULT_IMG = "https://kamparmedia.com/wp-content/uploads/2025/11/KamparMedia-Network-1-e1764288408541.png";// Decode HTML Special Entities (seperti &, –, dll)
function decodeHTML(htmlStr) {
if (!htmlStr) return "";
const txt = document.createElement("textarea");
txt.innerHTML = htmlStr;
return txt.value;
}// Format Tanggal Indonesia (contoh: 5 Nov 2026)
function formatDate(dateIso) {
if (!dateIso) return "";
const d = new Date(dateIso);
return d.toLocaleDateString("id-ID", { day: "numeric", month: "short", year: "numeric" });
}// Ambil Featured Image URL
function getFeaturedImage(post) {
if (post._embedded && post._embedded["wp:featuredmedia"] && post._embedded["wp:featuredmedia"][0]) {
return post._embedded["wp:featuredmedia"][0].source_url || DEFAULT_IMG;
}
return DEFAULT_IMG;
}// Ambil Nama Kategori Utama
function getCategoryName(post, fallback = "Berita") {
if (post._embedded && post._embedded["wp:term"] && post._embedded["wp:term"][0] && post._embedded["wp:term"][0].length > 0) {
return post._embedded["wp:term"][0][0].name || fallback;
}
return fallback;
}// Ambil Nama Penulis Redaksi
function getAuthorName(post, fallback = "Redaksi Newsroom") {
if (post._embedded && post._embedded["author"] && post._embedded["author"][0]) {
return post._embedded["author"][0].name || fallback;
}
return fallback;
}// 1. FETCH HEADLINE (Tag / Kategori: Liputan Khusus)
async function loadHeadline() {
try {
let endpoint = `${DOMAIN}/wp-json/wp/v2/posts?per_page=1&_embed=1`;
// Cari ID tag 'liputan-khusus'
const tagRes = await fetch(`${DOMAIN}/wp-json/wp/v2/tags?slug=liputan-khusus`);
const tagData = await tagRes.json();
if (tagData && tagData.length > 0) {
endpoint += `&tags=${tagData[0].id}`;
} else {
// Fallback pencarian ID kategori 'liputan-khusus'
const catRes = await fetch(`${DOMAIN}/wp-json/wp/v2/categories?slug=liputan-khusus`);
const catData = await catRes.json();
if (catData && catData.length > 0) {
endpoint = `${DOMAIN}/wp-json/wp/v2/posts?categories=${catData[0].id}&per_page=1&_embed=1`;
}
}const res = await fetch(endpoint);
const posts = await res.json();
let headlinePost = null;if (posts && posts.length > 0) {
headlinePost = posts[0];
} else {
// Fallback jika belum ada tag/kategori liputan-khusus, ambil 1 berita terbaru
const fallbackRes = await fetch(`${DOMAIN}/wp-json/wp/v2/posts?per_page=1&_embed=1`);
const fallbackPosts = await fallbackRes.json();
if (fallbackPosts && fallbackPosts.length > 0) {
headlinePost = fallbackPosts[0];
}
}if (headlinePost) {
document.getElementById("headline-img").src = getFeaturedImage(headlinePost);
document.getElementById("headline-badge").textContent = getCategoryName(headlinePost, "Liputan Khusus");
document.getElementById("headline-date").textContent = formatDate(headlinePost.date);
document.getElementById("headline-author").textContent = getAuthorName(headlinePost);
const titleElem = document.getElementById("headline-link");
titleElem.textContent = decodeHTML(headlinePost.title.rendered);
titleElem.href = headlinePost.link;// Bersihkan tag HTML dari excerpt
const rawExcerpt = headlinePost.excerpt ? headlinePost.excerpt.rendered : "";
const cleanExcerpt = decodeHTML(rawExcerpt.replace(/]+(>|$)/g, ""));
document.getElementById("headline-excerpt").textContent = cleanExcerpt;return headlinePost.id; // Kembalikan ID artikel untuk dikecualikan di grid kanan
}
} catch (err) {
console.warn("REST API Headline Notice:", err);
}
return null;
}// 2. FETCH 4 BERITA TERKINI (Grid Kanan)
async function loadPopularGrid(excludeId) {
const container = document.getElementById("popular-cards-container");
try {
let categoryQuery = "";
const catRes = await fetch(`${DOMAIN}/wp-json/wp/v2/categories?slug=berita-terkini`);
const catData = await catRes.json();
if (catData && catData.length > 0) {
categoryQuery = `&categories=${catData[0].id}`;
}let endpoint = `${DOMAIN}/wp-json/wp/v2/posts?per_page=5${categoryQuery}&_embed=1`;
if (excludeId) {
endpoint += `&exclude=${excludeId}`;
}const res = await fetch(endpoint);
const posts = await res.json();if (!posts || posts.length === 0) {
container.innerHTML = `
Belum ada berita terbaru.
`;
return;
}const targetPosts = posts.slice(0, 4);
let cardsHtml = "";targetPosts.forEach((post) => {
const title = decodeHTML(post.title.rendered);
const link = post.link;
const date = formatDate(post.date);
const imgSrc = getFeaturedImage(post);
const categoryName = getCategoryName(post, "Kampar");cardsHtml += `
${categoryName}
${date}
`;
});container.innerHTML = cardsHtml;
} catch (err) {
console.error("Gagal memuat berita terkini:", err);
container.innerHTML = `
Gagal terhubung ke REST API WordPress.
`;
}
}// Inisialisasi Otomatis
async function initHeroGrid() {
const headlineId = await loadHeadline();
await loadPopularGrid(headlineId);
}initHeroGrid();
});