Advertisement
  1. Code
  2. JavaScript
  3. jQuery

Create an FAQ Accordion for WordPress With jQuery UI

Scroll to top
Read Time: 5 min

Creating an FAQ section for your WordPress website is incredibly simple. We're going to use WordPress Custom Post Types for the questions & answers. Then we'll use a jQuery UI accordion to make a nice cross-browser accordion widget. Finally we'll assign a shortcode so that we can put our FAQ on any page or post.

We'll be creating this:


Step 1 Create the Directory and Files

  1. Create a new folder inside your theme folder called faq
  2. Inside the 'faq' folder, create a new file called faq.php
  3. Create another file called faq.js

Step 2 Include the faq.php File

In your functions.php (located in the root directory of your theme) – include the faq.php file you created at the top.

1
2
/* functions.php */
3
include('faq/faq.php');

Step 3 Create the Custom Post Type

  1. To register the Custom Post Type, we are going to hook into the init action. We are using an anonymous function as the second parameter to help keep everything encapsulated in one place. This helps with readability and maintainability.
  2. Set up $labels and $args as seen below.
  3. At the end we call register_post_type('FAQ', $args)
  4. Now if you go into your Admin area you will see a new option in the menu – FAQ (as seen in the image below)
  5. Click Add New Question and enter a few Questions and Answers so that we have something to work with later on. Use the title field for the question, and the main content field for the answer. This allows us to enter any type of content into our answer (such as images & videos) as well as text.
1
2
/* Register the Custom Post Type */
3
/* faq.php */
4
add_action('init', function() {
5
6
	$labels = array(
7
		'name' => _x('FAQ', 'post type general name'),
8
		'singular_name' => _x('Question', 'post type singular name'),
9
		'add_new' => _x('Add New Question', 'Question'),
10
		'add_new_item' => __('Add New Question'),
11
		'edit_item' => __('Edit Question'),
12
		'new_item' => __('New Question'),
13
		'all_items' => __('All FAQ Questions'),
14
		'view_item' => __('View Question'),
15
		'search_items' => __('Search FAQ'),
16
		'not_found' => __('No FAQ found'),
17
		'not_found_in_trash' => __('No FAQ found in Trash'),
18
		'parent_item_colon' => '',
19
		'menu_name' => 'FAQ'
20
	);
21
22
	$args = array(
23
		'labels' => $labels,
24
		'public' => true,
25
		'publicly_queryable' => true,
26
		'show_ui' => true,
27
		'show_in_menu' => true,
28
		'query_var' => true,
29
		'rewrite' => true,
30
		'capability_type' => 'post',
31
		'has_archive' => true,
32
		'hierarchical' => false,
33
		'menu_position' => null,
34
		'supports' => array('title', 'editor', 'page-attributes')
35
	);
36
	register_post_type('FAQ', $args);
37
});

Step 4 Include jQuery, jQuery UI, and faq.js

  1. Load jQuery
  2. Load jQuery UI
  3. Load the stylesheet for the jQuery UI library
  4. Load our custom script faq.js
1
2
add_action( 'wp_enqueue_scripts', 'wptuts_enqueue' );
3
function wptuts_enqueue() {
4
	wp_register_style('wptuts-jquery-ui-style', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/themes/south-street/jquery-ui.css');
5
	wp_enqueue_style('wptuts-jquery-ui-style');
6
7
	wp_register_script('wptuts-custom-js', get_template_directory_uri() . '/faq/faq.js', 'jquery-ui-accordion', '', true);
8
	wp_enqueue_script('wptuts-custom-js');
9
}

You'll notice we only used one wp_enqueue_script call, because it's important the JavaScript files are loaded in order as they are dependent on each other. Setting jquery-ui-accordion as a dependency makes sure this happens.


Step 5 Setup the Shortcode

Because we want to be able to put our FAQ Accordion on any page/post, we're going to generate a shortcode. Using a shortcode means that we only have to type [faq] inside any post/page in the WordPress Editor to display our FAQ.

1
2
add_shortcode('faq', function() {
3
	return "Shortcode test";
4
});

Step 6 Get the FAQ Questions & Answers

We can get the data from our custom post type by using the get_posts() function.

  1. numberposts – Here you can limit how many FAQ questions are retrieved
  2. orderby and order – Allows us to change the order of the questions
  3. post_type – This is how we tell WordPress to only fetch our custom post type
1
2
add_shortcode('faq', function() {
3
	$posts = get_posts( array(
4
		'numberposts' => 10,
5
		'orderby' => 'menu_order',
6
		'order' => 'ASC',
7
		'post_type' => 'faq'
8
	)); //array of objects returned

9
});
1
2
/* example */
3
echo $posts[0]->post_content; // will output the answer from the first faq question.

Step 7 Generate the Markup for the jQuery UI Accordion

This is the markup needed for the jQuery UI Accordion :

1
2
<!-- Markup needed for jQuery UI Accordion -->
3
<div id="wptuts-accordion">
4
	<h3><a href="">Question Will Go Here</a></h3>
5
	<div>Answer will be in this div.</div>
6
</div>

We can generate this by looping over the $posts array.

  1. First we use $faq to store the start of our HTML – we open up a div with an id of wptuts-accordion
  2. Next we start looping through all the posts and adding the result of sprintf to the $faq variable.
  3. sprintf will replace %1$s with the value retrieved from $post->post_title and %2$s with the value returned from $post->post_content
  4. We run $post->post_content through wpautop() to ensure it displays as it was authored in the admin area.
  5. Finally we close off the div and return $faq to output the HTML onto our page.
1
2
	$faq  = '<div id="wptuts-accordion">'; // the container, before the loop

3
4
	foreach ( $posts as $post ) {
5
		$faq .= sprintf(('<h3><a href="">%1$s</a></h3><div>%2$s</div>'),
6
			$post->post_title,
7
			wpautop($post->post_content)
8
		);
9
	}
10
11
	$faq .= '</div>'; // finish off by closing the container

12
13
	return $faq;

The Full Shortcode

1
2
add_shortcode('faq', function() {
3
4
	$posts = get_posts(array(  //Get the FAQ Custom Post Type

5
		'numberposts' => 10,
6
		'orderby' => 'menu_order',
7
		'order' => 'ASC',
8
		'post_type' => 'faq',
9
	));
10
11
	$faq  = '<div id="wptuts-accordion">'; //Open the container

12
	foreach ( $posts as $post ) { // Generate the markup for each Question

13
		$faq .= sprintf(('<h3><a href="">%1$s</a></h3><div>%2$s</div>'),
14
			$post->post_title,
15
			wpautop($post->post_content)
16
		);
17
	}
18
	$faq .= '</div>'; //Close the container

19
20
	return $faq; //Return the HTML.

21
});

Final Step

Phew! If you have got this far, well done – you're nearly there! At the moment we've managed to output all the data needed for our accordion, all that's left to do is place this in faq.js:

1
2
(function(){
3
	jQuery("#wptuts-accordion").accordion();
4
})();
Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.