- This topic has 25 replies, 4 voices, and was last updated 4 years, 8 months ago by
Alan.
-
AuthorPosts
-
July 19, 2021 at 2:28 pm #1861681
Alan
Hi.
I’ve created most of my webpages by using full width “sections” and I find it a really easy way to add content. I’ve got one page that has a FAQ section near the bottom and I want to shorten this up by just showing the questions and when the questions are clicked on the answer will show. What is the best way of adding an accordion if most of the page is constructed using sections. Can it be added to a section?
My website is
Gold Coast Electrician – Ceiling Fan installation
Thanks,
Regards
Alan
July 19, 2021 at 2:48 pm #1861699Leo
StaffCustomer SupportHi Alan,
Unfortunately the accordion feature requires a third-party plugin for now.
One of our users suggested this one a while back that might be worth a look;
https://wordpress.org/plugins/accordion-blocks/July 19, 2021 at 7:59 pm #1861908Alan
Hi Leo
Will that work within a “section” ? My whole page is constructed using sections
Thanks,
Alan
July 19, 2021 at 8:02 pm #1861913Leo
StaffCustomer SupportUnfortunately not.
There isn’t a solution that turns sections into accordions.
July 19, 2021 at 8:05 pm #1861922Alan
Would something like this work?
July 19, 2021 at 8:16 pm #1861931Elvin
StaffCustomer SupportHi Alan,
Yes that should work assuming you’ve placed the correct class selectors and loaded the script correctly. 😀
So basically, you can copy paste the JS and the CSS and add
accordionclass to the section wrapper so the script properly controls it.July 19, 2021 at 8:21 pm #1861933Alan
OK that’s great. I’m pretty familiar with the css and the classes but how do I add the JS? Do I just copy that exactly as it is and paste it somewhere?
Thanks
July 19, 2021 at 8:50 pm #1861970Elvin
StaffCustomer SupportOK that’s great. I’m pretty familiar with the css and the classes but how do I add the JS? Do I just copy that exactly as it is and paste it somewhere?
Thanks
You can use Hook Element for this.
Place this code on its code area:
<script> var acc = document.getElementsByClassName("accordion"); var i; for (i = 0; i < acc.length; i++) { acc[i].addEventListener("click", function() { /* Toggle between adding and removing the "active" class, to highlight the button that controls the panel */ this.classList.toggle("active"); /* Toggle between hiding and showing the active panel */ var panel = this.nextElementSibling; if (panel.style.display === "block") { panel.style.display = "none"; } else { panel.style.display = "block"; } }); } </script>You then set its hook to
wp_footerand set the display rule to the page where accordion sections are. 😀July 19, 2021 at 9:04 pm #1861991Alan
Thanks Elvin
I don’t think I’ve done that before but here goes 🙂
Is this correct?
Appearance – Generatepress – Elements – Add New Element – Hook
Put “accordion” in the Title
Copy and paste the script into the area below.
Settings – Hook – wp_footer – Everything else default (Execute Shortcodes unticked, Execute PHP unticked, Priority 10)
Display Rules – Location – Page – My ceiling fan page (exclude and User untouched)July 19, 2021 at 9:10 pm #1861996Elvin
StaffCustomer SupportYes that’s correct. You can link me to that pages to check and confirm if the hook is applied. 😀
July 19, 2021 at 9:30 pm #1862016Alan
OK I added it but when I look at the webpage I don’t have the expand + on the end of each question and the accordion doesn’t open. Its right at the end of the page.
My web address is Gold Coast Electrician – Ceiling Fans
I saw this. Is that script suitable for wordpress? Tom mentioned something but I’m not sure if it applies to my one
July 19, 2021 at 9:48 pm #1862035Elvin
StaffCustomer SupportThe HTML structure doesn’t match.
The accordion button is not a sibling of the panel as shown here – https://share.getcloudapp.com/z8uOjNrm
The button shouldn’t be wrapped on a
<p>element.July 19, 2021 at 11:06 pm #1862101Alan
I’m not sure where that <p> tag is coming from. I’ve got the exact code from the W3 Schools website
<button class="accordion">The question</button> <div class="panel"> <p>The answer</p> </div>Sorry I’m not sure what you mean by
“The accordion button is not a sibling of the panel”
That code is exactly what was at W3schools
July 19, 2021 at 11:23 pm #1862116Elvin
StaffCustomer SupportThe reference has this structure:
<button class="accordion">Section 3</button> <div class="panel"> <p>Lorem ipsum...</p> </div>Your site has this structure:
https://share.getcloudapp.com/5zuY6gd4I see that you’ve tried to correct it but there’s still empty <p> tags in between the accordion element and the panel element. These are likely from empty paragraph blocks. Use the list view on Gutenberg to find them and then remove them. https://share.getcloudapp.com/Z4uKXb6W
July 19, 2021 at 11:43 pm #1862132Alan
I just removed all of the html from within that “GeneratePress Section” and pasted the exact html from W3Schools but the result is the same. This is my html in the section.
<button class="accordion">Section 1</button> <div class="panel"> <p>Lorem ipsum...</p> </div> <button class="accordion">Section 2</button> <div class="panel"> <p>Lorem ipsum...</p> </div> <button class="accordion">Section 3</button> <div class="panel"> <p>Lorem ipsum...</p> </div> -
AuthorPosts
- You must be logged in to reply to this topic.