Creating an FAQs page - MB Views
If you are looking at how to create an FAQs page without using any page builder, we will share an easy and straightforward tip using MB Views that you can follow to build that page. Let’s dive in to explore the process of creating the FAQs page in detail.
This is the page I’d like to create today:
Video Version
Preparation
The FAQs page contains questions and answers that are saved in the custom fields created with Meta Box. The questions and answers will be classified and separated into tabs. In each tab, each pair of questions and answers also will be grouped together.
So, in this practice, we need these tools:
- Meta Box core plugin: to have a framework to create custom fields;
- MB Views: to create and style the template for the page without touching the theme files;
- Meta Box Builder: to have a UI on the back end to create custom fields;
- Meta Box Group: to organize the fields for questions and answers into a group since they are in pairs.
You can install them individually or use Meta Box AIO.
1. Creating a new page
Go to Pages > Add New to have a new FAQs page.
2. Creating custom fields
As I mentioned, the questions and answers will be classified in different tabs and also grouped in pairs. So, I’ll create the fields with the following structure.
The cover group for tabs is cloneable, so you can add as many tabs as you want for multiple kinds of questions.
You can also set the title for the group based on the content of the Tab Name field for easier identification.
Inside each tab, means the group, I create a field to name the tab. This name will be displayed on the left sidebar of the page.
Also, I added a subgroup that contains two subfields. Thanks to this subgroup, the questions and answers are grouped in a pair. The subgroup also is cloneable so you can add more pairs of questions and answers.
There are 2 text fields inside the subgroup. They are for the questions and answers.
This subgroup is set to be cloneable as well. It helps to add multiple pairs of questions and answers.
After configuring all the fields, move to the Settings tab. Choose Location as Post Type and select Page.
To apply these fields to the page you want, go to the Advanced Location Rules section below and choose the name of the page.
Now, go to the page editor, you will see the custom fields displayed.
3. Displaying the FAQs on the page
3.1. Getting and display Q&As from custom fields
Go to Views in Meta Box and create a new one.
To get the data saved in the custom fields, just click on the Insert Field button and choose the name of the created fields from the list.
Since the created fields are all the subfields in a group named Tabs. You must click on it to open it up. Whenever you click on it, a loop will be added to the template since the group is cloneable.
We’ll insert fields inside this loop. First, insert the Tab Name field into the loop.
The page I’ll create today has two sections. One on the left are the names of the tabs. And, the one on the right are the questions and answers. It is separated from the left one, so I will add another loop by clicking on the Tabs field.
The group containing the questions and answers is also cloneable, so there will be another loop inside.
Now, just insert the Question field and Answer field into the loop.
You can see the structure clearer like this.
You should add some div tags and classes to use CSS for styling later.
Now, set the template type as Singular, and choose the page as its location.
Go to the page on the front end, all the tab names, questions, and answers are displayed already.
That’s done for getting the questions and answers from custom fields and displaying them on the page.
3.2. Styling the page
Back to the template in the Views, go to the CSS tab, and add some code.
Then the look of the page will change.
For now, when you click on the name of each tab, nothing happens. All the pairs of questions and answers still display on the page. There is no classification. You should add JS to make the tabs work.
3.3. Adding JavaScript for tabs
Also back to the template, go to the JavaScript tab to add code.
jQuery(document).ready(function()
{
function activeTab(obj)
{
jQuery('.tab-category ul li').removeClass('active');
jQuery(obj).addClass('active');
var id = jQuery(obj).find('a').attr('href');
jQuery('.ul-cate').hide();
jQuery(id).show();
}
jQuery('.tab-category li').click(function(){
activeTab(this);
return false;
});
activeTab(jQuery('.tab-category li:first-child'));
});
These lines of code are to trigger when users click on any tab name. When I add div tags and classes for each element on the page, I set the class for the tab names as 'tab-category'. So, we’ll trigger the action based on that class.
I also set classes for the section displaying questions and answers and use it in this code.
If you use this code, please change these classes to your own.
Then, we need to add a line of code to the theme’s file to declare the JavaScript library
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
All of the code I’m using in this practice is uploaded on our Github channel, you can refer to them.
Now, go to the FAQs page on the frontend, you will see the tabs run as we want.