How To Add An Accordion In WordPress Site?
Last Updated :
29 Sep, 2024
Accordion is a web design element of WordPress. It displays text in a collapsed, stacked manner and has multiple sections or panels that expand upon click to disclose the hidden content, click again to revert back to its original state.
In WordPress, accordions can be added through the use of plugins or Gutenberg blocks and even custom code-it, therefore is a really versatile tool for improving navigation and layout on a site.
Below are the two approaches to add an accordion in a WordPress site:
Using the Easy Accordion plugin
Step 1: Go to the plugins section
From the WordPress dashboard, navigate to Plugins > Add New.
Add New PluginStep 2: Search and Install the Plugin
Search for Easy Accordion Plugin after click to Install.
Install PluginStep 3: Activate the plugin
Once installed, go to your Plugins web page and activate the Easy Accordion plugin.
Activate the pluginStep 4: Create a New Accordion
When you are finished, visit Easy Accordion > Add New in the dashboard.
Create a New AccordionStep 5: To Add an Accordion Group Title
Add the title for Accordion Group. Example "GeekForGeeks"
To Add an Accordion Group TitleStep 6: To Add Title and Content to the Accordion Items
Add a Title and content material to every accordion item.
Add Title and Content to the Accordion ItemsStep 7: Publishing Your Accordion Items
When your accordion listing is ready Click Publish to make those items to be had.
Publishing Your Accordion ItemsStep 8: Adding the Easy Accordion
Click the + icon to add a block, then search for Easy Accordion.
Adding the Easy Accordion Step 9: Insert the Easy Accordion
After finding it, your next step is drag and drop the Accordion block into your web page.
Insert the Easy AccordionOutput
Using HTML, CSS, JavaScript Code
Step 1: Log into Your WordPress Dashboard
Go to your WordPress site and sign in. Which will take you to the WordPress Dashboard, that is your site control panel.
Log into Your WordPress DashboardStep 2: Access the Theme Editor
Appearance > Theme Editor on the Dashboard sidebar. Here is where you will be putting your custom CSS and JavaScript for the accordion.
Access the Theme EditorStep 3: Add HTML for the Accordion to Your Page/Post
The next step is to include the HTML for the accordion in a WordPress post or page content area. Posts > Add New (or select of existing post).
Add HTML for the Accordion to Your Page/PostChange the editor from Visual to Text (without any of the editor styles) mode allowing HTML input).
Add HTML for the Accordion to Your Page/PostUse this accordion HTML where you want the accordion to appear:
HTML
<div class="accord">
<div class="accord-item">
<h3 class="accord-header">Accordion Section 1</h3>
<div class="accord-content">
<p>Hello GeekForGeeks Family 2.0 </p>
</div>
</div>
<div class="accord-item">
<h3 class="accord-header">Accordion Section 2</h3>
<div class="accord-content">
<p> Hello GeekForGeeks Family 2.0</p>
</div>
</div>
</div>
Save or Publish the post.
Save or Publish the post.The <h3> tags act as the headers for each accordion section. The content inside <div class="accord-content"> will be hidden or shown when the user clicks on the header.
Step 4: Add CSS for Accordion Styling
Now we will style the accordion using CSS. This CSS will keep the accord-content hidden initially, which will appear only when you click on the header. Customize under Appearance.
Add CSS for Accordion StylingSelect Additional CSS by clicking.
Add Additional CSSInput the css code with the css classes related to your theme in the Additional CSS box
CSS
/* Write CSS Here */
.accord-content {
display: none; /* Initially hide the content */
padding: 10px;
border-top: 1px solid #ccc;
}
.accord-header {
cursor: pointer; /* Changes cursor to pointer on hover */
padding: 10px;
background-color: #f1f1f1;
border-bottom: 1px solid #ccc;
font-weight: bold;
}
.accord-header.active + .accord-content {
display: block; /* Display content when the header is clicked */
}
Click Publish to save the CSS changes.
Publish to save the CSS changes..accord-content is hidden by default with display: none. When the user clicks on an accordion header (defined by .accord-header), the content (.accord-content) will be displayed. The cursor: pointer property makes the accordion header more interactive.
Step 5: Add JavaScript for Accordion Functionality
When users click on the headers, JavaScript will take care of the interactive features, such as expanding and collapsing the material.
Using the WPCode Plugin as an example:
- Go to Plugins > Add New and look for WPCode to install the plugin.
- Install and activate it.
Add JavaScript for Accordion Functionality- Go to Code Snippets > Click on the header and Footer.
Add JavaScript for Accordion FunctionalityPut your JavaScript code in the section marked "Scripts in Footer."
JavaScript
<script>
document.addEventListener('DOMContentLoaded', function() {
var headers = document.querySelectorAll('.accordion-header');
headers.forEach(function(header) {
header.addEventListener('click', function() {
this.classList.toggle('active');
var content = this.nextElementSibling;
if (content.style.display === "block") {
content.style.display = "none";
} else {
content.style.display = "block";
}
});
});
});
</script>
Click Save File to save the changes.
Save the changesClicks on the accordion headers (.accord-header) trigger this JavaScript code. It can be clicked to show or hide the matching content (.accord-content) by twiddling its display property. To maintain track of which header is open, the. Active class is added or removed. Once your WordPress website has been updated with HTML, CSS, and JavaScript, it is ready to use.
Add An Accordion In WordPress SiteOutput:
Similar Reads
How to Add Widgets in WordPress ? Adding widgets in WordPress is a straightforward way to enhance your website's functionality and design. Widgets are small blocks that perform specific functions, allowing you to add features such as calendars, search bars, recent posts, or custom menus to your WordPress site. They make your website
5 min read
How to create Accordion in ReactJS ? Accordions contain creation flows and allow lightweight editing of an element. Material UI for React has this component available for us, and it is very easy to integrate. We can create it in React using the following approach.Approach to create Accordion in React JSTo create accordion in react we w
2 min read
How to Create Image Accordion using HTML and CSS ? In this article, we will see how to create an image accordion using HTML & CSS that is basically used for advertising purposes on e-commerce websites. An Accordion is often used to open multiple sections at full width & also content pushes the page content downward.Approach:Create an HTML fi
2 min read
How to embed iFrame in WordPress ? An iFrame (Inline Frame) is a way to embed one web page inside another using HTML code. This allows you to display content like videos, maps, or other websites directly on your page without needing to store these files on your own server. iFrames are handy for showing external content while keeping
3 min read
How to Build Accordion Menu using JavaScript ? Accordion is used to render or hide, or toggle the view of the content with the help of adding the collapsible effect. It helps to manage and organize information in a compact and visually appealing manner. Accordion Menu may contain nested accordions, in order to render a large volume of content in
4 min read