GB 아코디언(Accordion) 블록은 ‘복수의 아이템 열기 유지(Keep multiple items open)’ 옵션을 꺼두기만 해도, 하나를 열면 다른 하나가 자동으로 접히는 전형적인 FAQ UI를 만들 수 있습니다.
하지만 쿼리 블록으로 CPT를 불러오면 구조가 달라집니다. 이때는 반복되는 각 항목이 하나의 독립된 아코디언으로 출력되기 때문에, 화면상으로는 하나의 FAQ 목록처럼 보여도 실제로는 하나의 아코디언 블록의 자식이 아니어서 자동으로 펼치기Expanded)나 접기(Collapsed)가 안됩니다.
① CSS 클래스 넣기
루퍼(Looper) 블록에 ‘loop--collapsed’라는 클래스를 추가합니다.
② JS 추가
<script>
document.addEventListener('DOMContentLoaded', function () {
const wrapper = document.querySelector('.faq-query-accordion');
if (!wrapper) return;
const items = wrapper.querySelectorAll('.gb-accordion__item');
items.forEach((item) => {
const toggle = item.querySelector('.gb-accordion__toggle, button');
if (!toggle) return;
toggle.addEventListener('click', function () {
setTimeout(() => {
items.forEach((otherItem) => {
if (otherItem !== item) {
otherItem.classList.remove('gb-accordion__item-open');
const otherToggle = otherItem.querySelector('.gb-accordion__toggle, button');
if (otherToggle) {
otherToggle.setAttribute('aria-expanded', 'false');
}
}
});
}, 10);
});
});
});
</script>Code language: JavaScript (javascript)