์๋ํ๋ ์ค ๊ฒ์๊ธ์ ๋๊ธ ๊ธฐ๋ฅ ๋ถ์ด๊ธฐ (REST API ๊ธฐ๋ฐ)
๐ ์๋ํ๋ ์ค REST API๋ฅผ ์ด์ฉํด์ ๋๋ง์ ์ฌ์ดํธ ๋ง๋ค๊ธฐ - 6ํธ
๊ฒ์๋ฌผ์ ๋๊ธ ๋ณด๊ธฐ & ์์ฑ ๊ธฐ๋ฅ ์ถ๊ฐํ๊ธฐ (๋๊ธ API ํ์ฉ)
๐งญ ๊ฒ์๋ฌผ ํด๋ฆญ → ์์ธ ํ์ด์ง๋ก ์ด๋ํ๋ ๊ตฌ์กฐ
ํฌ์คํ ์ ๋ฐ๋ก ํ์ง ์์์ง๋ง ๊ฒ์๋ฌผ ๋ชฉ๋ก ํ๋ฉด(index.html)์์ ๊ฐ ๊ฒ์๋ฌผ ํ๋จ์ ๊ฐ ๊ฒ์๋ฌผ์ ๋งํฌ๋ฅผ ํด๋ฆฝ๋ณด๋์ ๋ณต์ฌํ ์ ์๋ ๋ฒํผ๊ณผ, ํ์ฌ ๊ฒ์๋ฌผ์ ์์ฑ๋ ๋๊ธ์ ๊ฐ์๋ฅผ ๋ณด์ฌ์ง๋๋ก ๊ตฌํํด๋์ ์ํ์ ๋๋ค.
์ฌ๊ธฐ์ ๋๊ธ์ ํด๋ฆญํ๋ฉด id๋ฅผ ํฌํจํ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ๋ฅผ ์ด์ฉํด ์์ธ ํ์ด์ง๋ก ์ด๋ํ๋๋ก ์ค์ ํ์ต๋๋ค.
location.href = `post.html?id=${post.id}`;
๊ทธ๋ ๊ฒ ์ด๋ํ post.html์์๋ URL์์ id ๊ฐ์ ์ถ์ถํ์ฌ ํด๋น ๊ฒ์๋ฌผ์ ๋ถ๋ฌ์ต๋๋ค.
const postId = new URLSearchParams(location.search).get("id");
const postUrl = `https://myDomain.com/wp-json/wp/v2/posts/${postId}`;
์์ธ ํ์ด์ง(post.html) ๊ตฌ์กฐ:
<h3>๋๊ธ</h3>
<div id="commentList"></div>
<h4>๋๊ธ ์์ฑ</h4>
<textarea id="commentContent" placeholder="๋๊ธ์ ์
๋ ฅํ์ธ์"></textarea>
<button onclick="submitComment()">๋๊ธ ์์ฑ</button>
๐ฅ ๋๊ธ ๋ถ๋ฌ์ค๊ธฐ (GET):
์๋ํ๋ ์ค์ ๋๊ธ REST API๋ ์๋์ฒ๋ผ ๊ฐ๋จํ ํธ์ถํ ์ ์์ต๋๋ค.
const commentsUrl = `https://myDomain.com/wp-json/wp/v2/comments?post=${postId}`;
const res = await fetch(commentsUrl);
const comments = await res.json();
๊ทธ๋ฆฌ๊ณ ์ฐ๋ฆฌ๋ ์ด๊ฒ์ ์ด์ฉํด ๋๊ธ ํธ๋ฆฌ๋ฅผ ๊ทธ๋ ค์ค๋๋ค.
function renderComments(comments, parentId = 0, depth = 0) {
return comments
.filter(c => c.parent === parentId)
.map(c => {
const replies = renderComments(comments, c.id, depth + 1);
const indent = `margin-left: ${depth * 20}px`;
return `
<div class="comment" style="${indent}">
<div class="comment-author">${c.author_name}</div>
<div class="comment-content">${c.content.rendered}</div>
<button onclick="showReplyForm(${c.id}, ${depth + 1})">๋ต๊ธ</button>
<div id="reply-form-${c.id}"></div>
${replies}
</div>
`;
}).join("");
}
๐ ๋๊ธ ์์ฑ (POST)
async function submitComment(parentId = 0) {
const textareaId = parentId ? `replyContent-${parentId}` : "commentContent";
const content = document.getElementById(textareaId).value.trim();
if (!content) return alert("๋๊ธ์ ์
๋ ฅํด์ฃผ์ธ์.");
const res = await fetch("https://myDomain.com/wp-json/wp/v2/comments", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`, // ๋ก๊ทธ์ธ ํ ํฐ
},
body: JSON.stringify({
post: postId,
content,
parent: parentId,
}),
});
if (res.ok) {
document.getElementById(textareaId).value = "";
loadComments(); // ์๋ก๊ณ ์นจ ์์ด ๋๊ธ ๋ฐ์
} else {
const err = await res.json();
alert("๋๊ธ ์์ฑ์ ์คํจํ์ต๋๋ค: " + (err.message || "์ค๋ฅ"));
}
}
๋ต๊ธ์ ๋๊ธ์ ๋ฌ๊ธฐ์ํ ๋ต๊ธ ํผ ๋์ ์์ฑ:
function showReplyForm(parentId, depth) {
const container = document.getElementById(`reply-form-${parentId}`);
container.innerHTML = `
<textarea id="replyContent-${parentId}" placeholder="๋ต๊ธ์ ์
๋ ฅํ์ธ์"></textarea>
<button onclick="submitComment(${parentId})">๋ต๊ธ ์์ฑ</button>
`;
}
์์ฑ๋ ๋ชจ์ต
๐ ๋ง๋ฌด๋ฆฌํ๋ฉฐ
์ง๊ธ๊น์ง ์ด 6ํธ์ ๊ฑธ์ณ ์๋ํ๋ ์ค REST API๋ฅผ ํ์ฉํด ๋๋ง์ ์ฌ์ดํธ๋ฅผ ๋ง๋๋ ๊ณผ์ ์ ์ ๋ฆฌํด๋ณด์์ต๋๋ค.
์ฌ์ฉ์ ์ธ์ฆ๋ถํฐ ๊ฒ์๋ฌผ ์์ฑ, ์ด๋ฏธ์ง ์ ๋ก๋, ๊ฒ์, ๋๊ธ ๊ธฐ๋ฅ๊น์ง, ๊ธฐ๋ณธ์ ์ธ ๊ธฐ๋ฅ์ ์ด๋ ์ ๋ ๊ตฌํ๋์๊ธฐ์ ์ด ์ฐ์ฌ๋ ์ฌ๊ธฐ์ ๋ง๋ฌด๋ฆฌํ๋ ค ํฉ๋๋ค.
ํ์ง๋ง ์ฌ์ค, ์ง๊ธ๋ถํฐ๊ฐ ์ง์ง ์์์
๋๋ค.
์ด์ ๋ถํฐ๋ ๊ฐ์์ ์คํ์ผ์ ๋ง๊ฒ ์ฌ์ดํธ๋ฅผ ๋ง๋ค์ด๋๊ฐ๊ธฐ ๋๋ฆ์ ๋๋ค.
์๋ฅผ ๋ค๋ฉด ์ด๋ฐ ๊ฒ๋ค์
๋๋ค:
- โ๏ธ ๊ฒ์๋ฌผ ์์ /์ญ์ ๊ธฐ๋ฅ
- ๐ฌ ๋๊ธ ์์ /์ญ์ ๊ธฐ๋ฅ
- ๐จ ์ธํ ๊พธ๋ฏธ๊ธฐ (CSS ์ปค์คํฐ๋ง์ด์ง, ๋คํฌ๋ชจ๋ ๋ฑ)
์ฌ์ค 1~2ํธ ์ฐ๋ ๋์ ์ค์ ๊ตฌํ์ ๋๋ถ๋ถ ๋๋๋ฒ๋ ธ๊ณ , ๊ทธ ์ดํ๋ก๋ ๊ธ์ ์์ฑํ๊ธฐ์ํด์ ํต์ฌ๊ธฐ๋ฅ๋ง ๋นผ๋ด์ ์ฝ๋๋ฅผ ์์ฑํ์๋ ์คํ๋ ค ์ฝ๋๊ฐ ๊ผฌ์ด๊ฑฐ๋ ๊ท์ฐฎ์์ ๊ธ์ ์ฐ๋ ๊ฒ ์กฐ๊ธ ๋ฒ๊ฒ๊ธฐ๋ ํ์ต๋๋ค.
๊ทธ๋๋ ๋๊ตฐ๊ฐ์๊ฒ ์ด ์๋ฆฌ์ฆ๊ฐ ๋์์ด ๋์๊ธฐ๋ฅผ ๋ฐ๋ผ๋ฉฐ, ์ด๋ ๊ฒ ๋ง๋ฌด๋ฆฌํฉ๋๋ค.
์ฝ์ด์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค! ๐