Have you ever wanted to create a slick accordion FAQ page on your WordPress site? With jQuery, it‘s easy to build without writing any complex JavaScript.
In this beginner‘s guide, I‘ll show you how to create a jQuery accordion for FAQs in WordPress step-by-step.
After 15 years as a web developer, I‘ve found jQuery to be an invaluable tool for crafting modern interactivity. I want to share that experience with you today!
Let‘s dive in…
Contents
Why Use jQuery?
For anyone getting started with JavaScript, jQuery is a must-learn library. It makes front-end scripting much quicker and easier.
jQuery takes care of the tricky JavaScript internals, so you can focus on just the fun interactive parts.
For example, jQuery let‘s you:
- Select elements using CSS selectors
- Safely manipulate DOM elements
- Chain methods together in less code
- Build animations, accordions, and more
This is perfect for adding accordions, tabbed interfaces, image sliders, and other dynamic features to websites.
Here are a few simple examples of jQuery in action:
// Fade in an element
$(‘.my-element‘).fadeIn(‘slow‘);
// Toggle a section open and closed
$(‘.faq-question‘).click(function() {
$(this).next().slideToggle();
})
// Chain methods together
$(‘.button‘).on(‘click‘, function() {
$(this).fadeOut(‘fast‘).text(‘Goodbye!‘).fadeIn(‘slow‘);
})
Much easier than vanilla JavaScript!
According to W3Techs, jQuery is used on over 73% of all websites. This is because it allows you to build complex interactions in less time.
Now let‘s look at how to harness jQuery for a responsive FAQ page…
Why Use an Accordion for FAQs?
Accordions are a perfect way to present FAQ content. They keep your page clean and uncluttered by collapsing questions into small tabs.

Benefits of a FAQ accordion:
- Saves space by only showing one question at a time
- Easy to skim and navigate through questions
- Keeps structured and organized look
- Fun and interactive for visitors
Accordions also work great on mobile. Tabs would require too much scrolling and take up space.
The only downside is that accordions make it slightly harder to skim all questions at a quick glance. But the improved mobile experience is worth it for most sites.
Step 1 – Create a Plugin
To add a jQuery accordion to WordPress, we‘ll use a simple plugin. This makes the code reusable across any site.
Start by creating a folder named faq-accordion on your computer. This will house our plugin.
Inside, create a faq-accordion.php file and add this code:
<?php
/*
Plugin Name: FAQ Accordion
Description: Adds an accordion shortcode for FAQs.
Version: 1.0
Author: Your Name
*/
// Our accordion code will go here next
?>
This boilerplate provides the basic details for a valid WordPress plugin.
Now we can start building our accordion functionality…
Step 2 – Enqueue Scripts and Styles
Since WordPress includes jQuery already, we just need to load the jQuery UI interactions.
Add this code:
wp_enqueue_script( ‘jquery-ui-accordion‘ );
wp_enqueue_style( ‘jquery-ui‘, ‘https://example.com/jquery-ui.css‘ );
The first line will load the required jQuery UI JavaScript for the accordion effect.
The second line loads a jQuery UI theme CSS stylesheet to style the accordion components like tabs and arrows.
Step 3 – Build the Shortcode
Next, we‘ll register a shortcode that outputs the FAQ accordion markup:
function faq_shortcode() {
// Query FAQs here
// Generate accordion HTML
return $accordion_html;
}
add_shortcode( ‘faq_accordion‘, ‘faq_shortcode‘ );
This defines a faq_accordion shortcode to render our accordion.
Now let‘s look at querying FAQs and outputting the markup…
Displaying FAQ Posts
To show real FAQ posts in our accordion, we can use WP_Query to fetch the latest posts from a custom post type.
Add this inside the shortcode:
$args = array(
‘post_type‘ => ‘faq‘,
‘posts_per_page‘ => 10
);
$faqs = new WP_Query( $args );
This will grab the latest 10 FAQs to showcase.
Tip: I recommend organizing your FAQs in a custom post type rather than pages or posts. This gives you more flexibility.
Then loop through the results and output the accordion HTML:
while( $faqs->have_posts() ) {
$faqs->the_post();
$html .= ‘<h3>‘ . get_the_title() . ‘</h3>‘;
$html .= ‘<div>‘ . get_the_content() . ‘</div>‘;
}
This will populate the accordion with live FAQ posts!
Here is the full shortcode for reference:
function faq_shortcode() {
$args = array(
‘post_type‘ => ‘faq‘,
‘posts_per_page‘ => 10
);
$faqs = new WP_Query( $args );
$html = ‘<div id="faq-accordion">‘;
while( $faqs->have_posts() ) {
$faqs->the_post();
$html .= ‘<h3>‘. get_the_title() .‘</h3>‘;
$html .= ‘<div>‘. get_the_content() .‘</div>‘;
}
$html .= ‘</div>‘;
return $html;
}
add_shortcode( ‘faq_accordion‘, ‘faq_shortcode‘ );
This will generate a live accordion grid for your FAQ posts.
Customizing the Look and Feel
…
Let me know if you have any other questions!
