How to Create a FAQ Page with WordPress and Custom Post Types

In the web world, a FAQ page is created specifically for the viewers/customers, and contains general questions and their respective answers about a particular product or service. This tutorial details the process of creating a dedicated FAQ section in the WordPress backend with custom post types, as well as how to spice up the actual page a bit by using jQuery and CSS.

To accomplish our goal, we require a dedicated custom FAQ WordPress post type. That way, we can use the title and the content of these posts to display the FAQs in an innovative and user-friendly way.

Step 1: Installing WordPress

To begin creating the FAQ page, we, of course, must first install WordPress on our localhost. Installing WordPress is a piece of cake; however, if you're new to this, here is a guide that details the process.

The TwentyTen template is the default theme that ships with WordPress.

Once WordPress is ready to go, we should next create our custom theme, which will implement the FAQ functionality. There are various methods of creating a custom theme. Some prefer to create a new blank white template, while others like to create child themes of the new TwentyTen template. I have been using the Starker’s theme, by Elliot Jay Stocks for a long time now; we'll use it to create the new theme for our FAQ system.

Download the latest version of the Starker's blank theme, and move the folder into 'wp-content/themes', located within the WordPress installation folder. Also, be sure to rename it to FAQ. Next, login to the backend administration panel of WordPress, click on 'appearance/themes.' You will find that the default 'TwentyTen' theme is activated, while the new theme, 'Starkers,' is listed below it. Activate the Starkers theme.

activate starkers theme

Upon activation, preview the site to verify that everything is, indeed, working properly. If all went according to plan, the site should look somewhat like the following image:

blank starkers theme

Step 2: Implementing the FAQ Custom Post

To implement the FAQ system, we are going to create a custom post type solely for this purpose. This will enable us to manage all the FAQs in one place, especially if the FAQ question base increases with time.

To implement a custom post, edit the functions.php file located in the FAQ theme folder. It will contain a good bit of code, so don’t be scared or confused. Scroll down to the bottom and append the following to add a new custom post. We begin by creating hooking a custom function to the initialization (init) action.

This custom function will contain all the metadata for the custom post, and will also register the custom post within the WordPress database. Now, within the function, we are first going to define the labels which will be used in the backend administration panels. By labels, I mean the text that is going to show up in the user interface for adding, editing, and searching the FAQs in the admin panel.

After we've defined the labels, we next define the arguments array for the register_post_type method. These arguments contain all the important information which will define the components of our FAQ post. For example, will it have a tag box; an excerpt box? We pass the array of labels defined above as an argument, as well.

Now that the arguments are defined, we can register the custom post type using the register_post_type method. You can learn more about this method by referring to its documentation in the WordPress Codex.

Check the administration panel to determine if the FAQ type post has, in fact, been added successfully. Hopefully, you'll see the FAQ tab in the sidebar.

Custom post added to dashboard

Dummy FAQ Posts

Now go ahead and add some demo FAQs, because we need some data for creating and testing the template. The title of each FAQ post should be the question, and the content will be the answer.

Adding some demo posts

Step 3: Coding the Template

So far, we've created FAQ custom posts, as well as inserted a set of sample data. Now, we'll code the template to display the FAQs, accordingly. The main logic I have used for organizing the template is: use the FAQ's unique ID to link the question to the answers. Hence, we have two parts in the template: the questions section, listing all the FAQ titles; and the answer section, which displays the content of each of the FAQs.

Find the header.php file, open it, delete the div with an id of "access" at the bottom, and also the paragraph which contains the description of the blog. Now add the following code.

After we retrieve our FAQ post data, we must frame the architecture of how the questions will be displayed. We shall do it in the following manner. All the content is wrapped within a div with an id of "content."

The most important part here is where we assign the hyperlink with a value of '#answer' and append the post’'s id to it. We can use this to jump to the answers, when clicked.

After we've displayed all of the questions, we "rewind" our posts to return to the beginning.

Now we will structure how the answers are going to appear, just below the questions.

You can see that we are going to list the content of each post in a list element. Each list element will have an id of "answers" with the post ID appended to it. This is important: when the question is clicked, the view jumps to the content of the respective post. If you're working along, preview your site; you should see all the posts listed in the architecture described above.

FAQ displated without any styling

Step 4: Styling the Template

Styling of the FAQ page depends entirely on your tastes; you can proceed in any manner you wish. If you're a designer, feel free to skip Step 4. What I have personally implemented is a smooth and clean layout. When the user clicks on the question, the page smoothly scrolls down to the respective answer and highlights it by changing the colour and increasing the font size. To achieve this, we'll start by styling the template with CSS. We can make use of CSS3 to add some shadows and transition effects, too. Add the following CSS to styles.css.

After styling the page, we should style the current FAQ. Note that we've also added a 'Top' button to the current FAQ. To create the button, we'll use a handful of CSS3 properties.

Check if the 'current' class is working properly by assigning the class to any of the list elements. The current FAQ should look like the following:

Current Class Styling

Step 5: Adding a Pinch of jQuery UI

We'll use jQuery UI to add some effects to the page. You can download jQuery UI here. We only need limited use of the whole UI library, hence, downloading only the UI core components will suffice. We also need the jQuery scrollTo plug-in for achieving the smooth scrolling effect -- though you could also easily code this functionality on your own. Nonetheless, to save time, you can download the plug-in here.

First, we reference jQuery, the jQuery UI Core files, and the scrollTo plug-in within the header.php file. You can do this by adding the following code just before the wp_head() method.

The wp_enqueue_script statement is needed in order to load jQuery safely.

To enable our desired functionality, we fetch the value of the href attribute from the clicked element (i.e. the question). This value is the id of the list element which contains the answer. Then, we scroll to the list element, and apply the 'current' class. jQuery UI will ensure that the class is implemented on the list element smoothly and dynamically.

As mentioned earlier, we also have a 'Top' button which scrolls the page back to the top.

Step 6: Conclusion

What you've learned today is merely one way of implementing a FAQ page. WordPress provides the power of custom fields, which can be used to further improve the functionality of the FAQ system. When it comes to adding other features to our FAQ page, your own creativity is the only limit! Feel free to share your ideas in the comments!



Related Articles