ACF repeater fields as accordions in Breakdance

ACF repeater fields as accordions in Breakdance

The Accordion Element

Currently, The Breakdance accordion element doesn’t allow dynamic fields. This can change in the future, but at the moment, that’s not the case. Also, for the sake of completion, Destiny elements has an accordion element that allows the usage of ACF repeater fields, so that’s great.

That means, if you already have a membership, you won’t need this tutorial, BUT if you’re looking for just this one feature, or if you want to learn to do it yourself, this tutorial will show you how.

One more thing, this is not a custom element that was built in the Breakdance element studio, this is just an on-page, straightforward way of doing it.

Quick Note:

My usual approach to solving problems comes from an agency perspective. When you’re working with multiple clients, multiple timelines, delivering on deadline is super important, so writing or implementing reliable code that steps outside of what the builder does is a necessary skill because you really don’t have time to wait around for the plugin/theme developers to add your specific feature.

The Video

Timestamps

  • 0:04 Introduction
  • 1:15 Proof of concept
  • 2:10 Setting up ACf fields
  • 4:10 Setting up a global block
  • 9:20 Making the FAQ page
  • 15:05 Extra styling tips

The code

<script>
document.addEventListener('DOMContentLoaded', function () {
    const accordions = document.querySelectorAll('.ai-accordion');

    accordions.forEach(function (accordion) {
        const items = accordion.querySelectorAll('.bde-dynamic-repeater-item');
        
        items.forEach(function (item) {
            const toggleTitle = item.querySelector('.ai-toggle_title');
            const toggleContent = item.querySelector('.ai-toggle_content');
            const toggleChevron = item.querySelector('.ai-toggle_chevron');
            
            toggleContent.style.maxHeight = '0';
            toggleContent.style.overflow = 'hidden';
            
            toggleTitle.addEventListener('click', function () {
                const isOpen = toggleContent.style.maxHeight !== '0px';
                
                items.forEach(function (otherItem) {
                    const otherContent = otherItem.querySelector('.ai-toggle_content');
                    const otherChevron = otherItem.querySelector('.ai-toggle_chevron');
                    const otherTitle = otherItem.querySelector('.ai-toggle_title');
                    
                    if (otherContent !== toggleContent) {
                        otherContent.style.maxHeight = '0';
                        if (otherChevron) {
                            otherChevron.style.transform = 'rotate(0deg)';
                        }
                        if (otherTitle) {
                            otherTitle.classList.remove('toggle-open');
                        }
                    }
                });
                
                if (isOpen) {
                    toggleContent.style.maxHeight = '0';
                    if (toggleChevron) {
                        toggleChevron.style.transform = 'rotate(0deg)';
                    }
                    toggleTitle.classList.remove('toggle-open');
                } else {
                    toggleContent.style.maxHeight = toggleContent.scrollHeight + 'px';
                    if (toggleChevron) {
                        toggleChevron.style.transform = 'rotate(180deg)';
                    }
                    toggleTitle.classList.add('toggle-open');
                }
            });
        });
    });
});
</script>
<style>
.ai-toggle_title {
	cursor: pointer;
}
.ai-toggle_chevron,
.ai-toggle_content {
	transition: .5s all ease;
}
</style>

Post credit scene

This image was seriously considered to be used as the thumbnail, which ultimately, I decided against. BUT I think you should still be able to enjoy it.

Edit:

This is some additional tips on getting the repeaters to work on product categories. (Dim in the comments and I had a chat on FB about this)

The code screenshots

Hope this helps anyone who needs to add repeaters to a product category.

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. Dim says:

    Can this work for product categories with different FAQs for each one?

    • PK says:

      Hi Dim, yes it can! You can add the repeater field to ‘posts > products’ on the ACF field group and do the same on the product template.

      • Dim says:

        It doesn’t seem to work properly in WooCommerce product categories to be able to fetch multiple titles and content.
        The repeater field with the global block is empty.
        Breakdance | Version 2.0.0
        Advanced Custom Fields PRO | Version 6.3.4

New tutorials

ACF repeater fields as accordions in Breakdance
Breakdance 2 vs. Divi 4
Breakdance 2 for Agencies
First Look at Breakdance v2.0

Why no ads?

Hi, I'm PK, and I maintain and publish content on this site for two reasons.

  1. To give back to the community. I learned so much from people's tutorials and StackOverflow that I wanted to contribute and help others.

  2. To provide a more structured learning experience. People struggle to find the course that guides them from start to finish, based off of real life experience, and my courses provide that.

The only "ads" I have here are for my own courses, and maybe an affiliate link, but that's it. They fund the website resources and provide more motivation for me to produce better content.

Any bit of interest helps. Even sharing with your friends, suggesting my courses to your developer or designer, or subscribing to my YT channel, or joining our Discord. Thanks and I'll see you around!

There's a newsletter!

Sign up to the newsletter for the occasional updates on courses, products, tutorials, and sales.