Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Accordion – Open the first Accordion on Query Loop

  • Hi,

    So i need help on getting the first Accordion opened automatically, because i will be using the Accordion on Query Loop block.

    Block Selection

  • Hi there,

    see here:

    https://docs.generateblocks.com/article/accordion-overview/#block-settings-–-accordion-item

    Each of the Accordion Item blocks has a block setting option to be Open by Default.

  • Thanks, as it is query loop, i have one accordion in the post template. How can achieve this?

    Container style

    Block setting

  • any update?

  • Sorry your last reply slipped through our system.
    Hmmm… theres no option for that, as effectively each Accordion it outputs is its own accordion block, that all share the same identical settings.

    The workaround would be:

    1. Close the items by default.
    2. In a HTML block after the Query Loop add this script:

    
    <script>
    var accordionItem = document.querySelector('.gb-accordion__item');
    if (accordionItem) {
        var button = accordionItem.querySelector('button');
        if (button) {
            button.click();
        }
    }
    </script>
    

    That will open the first accordion for you.

  • Added it like this, but no luck.
    screenshot

  • The HTML block with the script has to be after the query loop / accordions. Not before it.

  • sorry like?
    screenshot

  • thats correct.

  • however its not working, when the page is loaded only want the first one opened.

  • Hi there,

    Try replacing David’s code to this:

    <script>
       var accordionItem = document.querySelector('.gb-container-48972393');
       if(accordionItem) {
          accordionItem.classList.add('gb-accordion__item-open');
       }
    </script>
  • great thanks, however i only want one item open at time, is that possible?

  • Unfortunately, that would require extensive JavaScript customization, and it’s beyond the scope of what we cover.

    Does the accordion really need to be displayed using a query loop? It seems like a single Accordion block would be sufficient as it allows for more control, but perhaps it is not feasible in your use case.

  • If that’s the case, all good. thanks so much for the help guys.

    The use of query loop is because i have FAQ as Custom Type Post, filtered by tags

  • No problem at all!

Viewing 15 posts - 1 through 15 (of 15 total)
  • You must be logged in to reply to this topic.