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.

Update

Breakdance v2.3 has added the function to allow accordions in their repeater fields. So… this tutorial is now… obsolete haha

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.

Get Breakdance

Breakdance is, in my opinion, the best page/site builder on the market, which is why all my tutorials use Breakdance. It's also why I have an affiliate link. You don't pay extra, but I get a little commission, so it helps support the site. Thanks!

Get ACF Pro

Another essential plugin for site building. This isn't an affiliate link, it's just for convenience.

Get WP Codebox Pro

If you'd like an easier to use, and more advanced code management plugin (instead of just editing and making files yourself) then WP codebox pro is for you. Adding php, js, and even SCSS. Pretty nice! Affiliate link below.

Blank Plugin

Sort of blank. It's made to be easy to use with the tutorials. Download and edit any way you see fit.
  1. Can this work for product categories with different FAQs for each one?

    • 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.

      • 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

Leave a Reply

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

New tutorials

Auto-updating Year in Footer
Designing Beyond the Brief
Disable Gutenberg Editor on some ACF pages
ACF flexible content fields in Breakdance 2.4

Got a question? ask!

If you have any question about the content on this page, feel free to send me an email by clicking the button below.

newsletter? sign up!

Sign up to the newsletter for updates on tutorials, news, and offers.

Join the cypher!

An awesome membership program for Breakdance Builder users coupled with an inclusive, friendly community.

Got a question?

If you have a question about something on this page, send me an email, and hopefully I can answer, and we can solve it!