How to Create a Team Members Page - P4 - Just Meta Box
Published: 2022-07-13 03:15:35

In the previous tutorials, we used Meta Box and page builders to create a team members page. However, if you don’t use any of them or want to find another way to do it, this post is for you.

In this practice, we’re going to use a new method - using Meta Box only with MB Views.

<!--more-->

This is the sample:

<img class="aligncenter" src="https://i.imgur.com/TXY7fCC.png" alt="Sample for the Team Members Page with Meta Box only." width="1000" height="562" />
<h2>Video Version</h2>
https://youtu.be/u6XVlG1_3gY
<h2>Before Getting Started</h2>
Similar to previous tutorials in this series, each member’s information will be preserved in a post and displayed as a name card. The member's name and avatar are the title and featured image of the post, respectively. Other information such as position, Facebook, Instagram, and Mail will be saved in custom fields.

To do it, we need Meta Box and its extensions:
<ul>
 	<li><a href="https://wordpress.org/plugins/meta-box/">Meta Box core plugin</a>: This is the framework to create custom fields and custom post types. It’s free and available on <a href="https://wordpress.org/plugins/meta-box/">wordpress.org</a>;</li>
 	<li><a href="https://metabox.io/plugins/custom-post-type/">MB Custom Post Type &amp; Custom Taxonomies</a>: It’s a free extension of Meta Box to create custom post types;</li>
 	<li><a href="https://metabox.io/plugins/meta-box-builder/">Meta Box Builder</a>: This Meta Box premium extension provides a UI in the back end to create custom fields easily;</li>
 	<li><a href="https://metabox.io/plugins/mb-views/">MB Views</a>: it’s an extension of Meta Box to create WordPress templates without touching the theme’s file as well as any page builders. It also generates a shortcode so that you can display the templates anywhere you want.</li>
</ul>
<h2>Step 1: Create a New Post Type</h2>
We need to create a custom post type and custom fields for the post type. If you read previous posts in the series, just skip this step.

In the <strong>Admin Dashboard</strong>, go to <strong>Meta Box</strong> &gt; <strong>Post Types</strong>, then create a new post type for the members.

<img class="aligncenter" src="https://i.imgur.com/eaUVoER.png" alt="Create a new custom post type for the Team Members" width="1000" height="534" />

Publish and you will see a new menu in your dashboard.

<img class="aligncenter" src="https://i.imgur.com/vrcT9BN.png" alt="The result appears after creating." width="1000" height="570" />
<h2>Step 2: Create Custom Fields for the Post Type</h2>
Let’s create custom fields for the position, Facebook, Instagram, and mail. Go to<strong> Meta Box </strong>&gt; <strong>Custom Fields</strong>.

<img class="aligncenter" src="https://i.imgur.com/pU2U2Wn.png" alt="Add new fields" width="1000" height="800" />

For the member’s information, I’ll create some following fields:
<table>
<tbody>
<tr>
<td>Fields</td>
<td>Type</td>
<td>Note</td>
</tr>
<tr>
<td>Position</td>
<td>Select</td>
<td>To add some options for users to choose from instead of typing.</td>
</tr>
<tr>
<td>Facebook/Instagram/Mail</td>
<td>URL</td>
<td>To save the link of the social networks</td>
</tr>
</tbody>
</table>
<img class="aligncenter" src="https://i.imgur.com/ouMfsKe.png" alt="Custom fields for Team Members" width="1004" height="604" />

In the event that you want to add more information, just create additional fields as you go.

After creating all the fields, move to the <strong>Settings</strong> tab of the field group. In the <strong>Location</strong> section, choose the <strong>Post Type</strong> and select <strong>Team Member</strong> to apply these fields to this post type.

<img class="aligncenter" src="https://i.imgur.com/sqmVZ9x.png" alt="Set location for the custom field as the created post type" width="1000" height="608" />

Now, you will see all the created fields when creating a new post in the <strong>Team Member</strong> post type. Add some member’s information for the test. We need them for the preview in the next step.

<img class="aligncenter" src="https://i.imgur.com/hrepb82.png" alt="The custom fields display in the post editor" width="1003" height="608" />
<h2>Step 3: Create a Template using MB Views</h2>
In the Admin Dashboard, go to <strong>Meta Box</strong> &gt; <strong>Views</strong> &gt; <strong>Add New</strong> to create a new template.
<img class="aligncenter" src="https://i.imgur.com/n4xngkG.png" alt="Create a new template." width="1000" height="642" />

In the <strong>Template</strong> tab of the view, add some code:

```
{% set args = { post_type: 'team-member', posts_per_page: -1, orderby: 'date', order: 'ASC' } %}
{% set posts = mb.get_posts( args ) %}
{% for post in posts %}
    {{ mb.get_the_post_thumbnail( post.ID,'medium' ) }}
    {{ post.facebook }}{{ post.instagram }}{{ post.mail }}
    {{ post.post_title }}
    {{ post.post_content }}
    {{ mb.rwmb_the_value( 'position', '', post.ID, false ) }}
{% endfor %}
```
<img class="aligncenter" src="https://i.imgur.com/TQW38fy.png" alt="Add new code to the template." width="1000" height="484" />

Let me explain about this code:

```
{% set args = { post_type: 'team-member', posts_per_page: -1, orderby: 'date', order: 'ASC' } %}
```
This code is used to declare the variable that obtained posts from the Team Member post type. The <code>posts_per_page: -1</code> means that it will get all the posts and display them in one page. The <code>orderby: 'date', order: 'ASC'</code> are used to define the order of the posts.

```
{% set posts = mb.get_posts( args ) %}
```
This function is to get the posts data from the variable that was declared above.

```
{% for post in posts %}
```
This is a loop to display all the posts in the Team Member post type.

```
{{ mb.get_the_post_thumbnail( post.ID,'medium' ) }}
```
This function is used to get the avatar of the member which is the featured image of the post.

```
{{ post.facebook }} | {{ post.instagram }} | {{ post.mail }} | {{ post.post_title }} | {{ post.post_content }}
```
This kind of function is used to get the data from the fields including both default fields of WordPress and custom fields. <code>facebook, instagram, mail</code> is the ID of the corresponding fields

For the position information, it is saved in a field in the type of <strong>Select</strong> with a special kind of data. So we use the following function to get its data.

```
<pre>{{ mb.rwmb_the_value( 'position', '', post.ID, false ) }}</pre>
```
The <code>rwmb_the_value()</code> is to get the label of the options in the Select field. In the event you want to get the value of the options, use this <code>rwmb_meta()</code>
To style the template, I used some Div classes and added them to the code.

<img class="aligncenter" src="https://i.imgur.com/Yhl503n.png" alt="Use some Div class to style the template." width="1000" height="735" />

You can refer to all the code <a href="https://github.com/wpmetabox/tutorials/blob/master/create-a-team-members-page-with-mb-views/template.php">here</a>.

After adding code, move to the <strong>Settings</strong> section, set this template as the <strong>Singular</strong> type and choose <strong>Location</strong> as <strong>Page</strong>. Then, select <strong>Meet The Team</strong> page to apply this template to the page.

<img class="aligncenter" src="https://i.imgur.com/N2KLyvZ.png" alt="Adjust the template and apply to the page." width="1000" height="628" />

Go to the Meet The Team and you will see all the data has been displayed already.

<img class="aligncenter" src="https://i.imgur.com/2M1sBbQ.png" alt="Result after applying the template." width="1000" height="562" />
<h2>Step 4: Style the Page</h2>
Still in the created template in the <strong>Views</strong>. Move to the <strong>CSS</strong> tab, then add some CSS code to style the page.

<img class="aligncenter" src="https://i.imgur.com/GAbooNk.png" alt="Style the page." width="1000" height="734" />

All the CSS code that I used is on <a href="https://github.com/wpmetabox/tutorials/blob/master/create-a-team-members-page-with-mb-views/custom.css">Github</a>. Refer to it for more details.

Update it and see the result on the frontend.

<img class="aligncenter" src="https://i.imgur.com/TXY7fCC.png" alt="Result of Team Members Page on the frontend." width="1000" height="562" />
<h2>Last Words</h2>
We’ve gone through many methods to create a team members page. Hope that this series is helpful for you even if you are using page builders or not.

If you have any questions or ideas for any further tutorials, feel free to leave a comment. Good luck!