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 Menu in WordPress?
WordPress is an open-source Content Management System that is based on PHP and MySql which is used to create a dynamic website. Meta Tag generates codes or tags for search engines to pick up your page titles and meta descriptions. These are HTML codes used to describe the content of a page. In this
2 min read
How to Add a Link to Wordpress Page?
Adding links to your WordPress pages is important any website. Links can connect different parts of your site, guide users to additional resources, or direct them to your resources. This guide will walk you through the steps to add a link to a WordPress page, ensuring your content is engaging and SE
3 min read
How to Add JavaScript in WordPress Page and Post?
WordPress doesnât allow you to directly add JavaScript code to your site, you can achieve this by using plugins or manual methods. JavaScript is a popular programming language that allows you to add interactive features to your website. Learning how to add JavaScript to your WordPress site can help
3 min read
How to Create HTML Sitemap in WordPress ?
Creating an HTML sitemap in WordPress can significantly improve your site's navigation and SEO. HTML sitemaps provide a user-friendly way to list all the pages of your website in a structured manner. This helps visitors find content easily and search engines to crawl your site more efficiently.What
3 min read
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 Columns in WordPress?
WordPress is a powerful platform that offers a range of features to help you create a professional-looking website. One of the most useful features is the ability to create columns, which can help you organize your content and make it more visually appealing. In this article, we will see the complet
2 min read
How to add a PHP page to WordPress?
Adding a custom PHP page to your WordPress site can be useful for various reasons, such as adding unique functionality or creating custom templates. This article will walk you through the process step by step.1. Create a WordPress Template PageStep 1: Login to Microsoft Web Matrix:Open or create you
2 min read
How to Create a Table of Contents in WordPress?
A Table of Contents (TOC) is an essential feature for any long-form content on your WordPress site. It enhances readability by allowing readers to quickly navigate to specific sections. In this article, weâll walk you through the steps to create a Table of Contents in WordPress.Why Add a Table of Co
2 min read
How to Create WordPress Plugin from Scratch ?
Creating a WordPress plugin from scratch might seem tough, but it's an essential skill for customizing and extending the functionality of your WordPress site. This article will walk you through the steps of creating a simple plugin.What is a WordPress Plugin?A WordPress plugin is a piece of software
5 min read
How to add a video in WordPress Posts or Pages ?
WordPress is an open-source Content Management System and an interface that is totally based on PHP and MySql which is used to create a dynamic website. Matt Mullenweg developed WordPress and Written in PHP language. WordPress is one of the most popular interfaces that allow users to customize and m
3 min read