Sometimes you need to display the same information with the same layout as well, but in different places. Instead of storing and getting that data several times, today we are going to use MB Views to create template parts, then include them in multiple templates for reusable purposes.
Regularly updating the homepage brings fresh content and helps impress website visitors easier. However, it may take time and effort to change its content. You definitely can avoid it with the help of custom fields created with Meta Box and MB Views. Let’s say goodbye to the hassle of coding in theme's files!
If you have an e-commerce website, you may want to provide your physical store address to help customers reach it more quickly and easily. So, you should insert a map to your website. It’ll be more attractive and more interactive than just writing an address in text.
Normally, there are 2 popular ways to add a map to show the locations: It’s embedding Google Maps scripts directly, and another way is using a plugin. However, they both do not work in the case that you are having a listing page site with a long list of different stores, and each one needs to have its own map. Then, the custom fields with Meta Box come handy in this way.
Normally, when you use Yoast SEO on your site, there will be a section to fill in the title, and description that helps search engines index them easier. You can fill in static content or dynamic content in these sections. But in some cases, you may want to get content not only from those default fields but from custom fields. So, how to add content from custom fields created with Meta Box to Yoast SEO meta tags, just follow these practices.
WooCommerce is the most popular eCommerce WordPress platform that accounts for more than 23% of the total number of eCommerce websites over the world. This platform helps users have online stores with common managing tools such as creating a cart, shipping management, order management, payment, order, tax, etc.
Sometimes you may want to embellish the fields, or have some signatures for them. There is not only a way to do that such as adjusting size, space, color, … but also adding icons into their title. Instead of using only text for the meta box’s title or field name, we can use icons for addition or alternative. Let’s dive in to explore the process of adding icons to the title in detail.
Among the various types of online advertising, classified advertisement is probably the earliest-developed type that still exists. Over the years, the classified websites have improved its interface and features, but they are still based on the basic features which are:
Allows users (sellers) to register an account, post a product or service with detail information;
Allows the audience (buyers) to search and view the products.
One of the most reputed websites in classified advertising is Ebay.com. I'm going to show you howto create a classified ads website in WordPress using Meta Box which follows Ebay’s model.
As you know, by default, WordPress comes with two post types: page and post. If you want a more variety of suitable post types to choose from, e.g., a post type for products, you have to create them yourself. Such post types are called Custom Post Types.
In this post, I am going to share with you two ways to create custom post types in WordPress. One uses codes, one uses plugins. Of course, the way using plugin seems like easier because of touching no code.
Let’s see how to do it.
Create Custom Post Types Using Code
Manually register a new custom post type
We need to hook to the init action to let WordPress register post type. We need the code below in the theme’s function.php:
Inside the prefix_create_custom_post_type() function is the register_post_type() the function which serves the purpose of creating a custom post type with parameters declared with $args.
Add parameters for your custom post type
There are many arguments that can be passed to $args and things can get quite complicated. For that reason, make sure to know what you need in order to choose sufficient arguments. To understand the WordPress register_post_type function paramerters, click here.
In this post, I have just created a simple custom post type, so I am using some basic parameters and arguments. Once you’ve clearly marked out what you want, proceed below.
With the same code structure as the one in the 1st step, but I use two additional parameters which are $labels and $supports. Furthermore, I am going to declare the $args with more details.
The $labels parameter is insignificant, you don’t have to pay too much attention to it. But, it will help you recognize the post type in the admin area.
The $supports parameter is used for declaring that the post type is supported with title, editor, excerpt, featured image, etc.
The $args parameter is used for including all the above arrays and some other important arguments as well.
You may learn more about the argument and its expression here.
function prefix_create_custom_post_type() {
/*
* The $labels describes how the post type appears.
*/
$labels = array(
'name' => 'Products', // Plural name
'singular_name' => 'Product' // Singular name
);
/*
* The $supports parameter describes what the post type supports
*/
$supports = array(
'title', // Post title
'editor', // Post content
'excerpt', // Allows short description
'author', // Allows showing and choosing author
'thumbnail', // Allows feature images
'comments', // Enables comments
'trackbacks', // Supports trackbacks
'revisions', // Shows autosaved version of the posts
'custom-fields' // Supports by custom fields
);
/*
* The $args parameter holds important parameters for the custom post type
*/
$args = array(
'labels' => $labels,
'description' => 'Post type post product', // Description
'supports' => $supports,
'taxonomies' => array( 'category', 'post_tag' ), // Allowed taxonomies
'hierarchical' => false, // Allows hierarchical categorization, if set to false, the Custom Post Type will behave like Post, else it will behave like Page
'public' => true, // Makes the post type public
'show_ui' => true, // Displays an interface for this post type
'show_in_menu' => true, // Displays in the Admin Menu (the left panel)
'show_in_nav_menus' => true, // Displays in Appearance -> Menus
'show_in_admin_bar' => true, // Displays in the black admin bar
'menu_position' => 5, // The position number in the left menu
'menu_icon' => true, // The URL for the icon used for this post type
'can_export' => true, // Allows content export using Tools -> Export
'has_archive' => true, // Enables post type archive (by month, date, or year)
'exclude_from_search' => false, // Excludes posts of this type in the front-end search result page if set to true, include them if set to false
'publicly_queryable' => true, // Allows queries to be performed on the front-end part if set to true
'capability_type' => 'post' // Allows read, edit, delete like “Post”
);
register_post_type('product', $args); //Create a post type with the slug is ‘product’ and arguments in $args.
}
add_action('init', 'prefix_create_custom_post_type');
Save that then go back to the admin dashboard. You will get something like this:
A new menu named Product has appeared in the left panel. This is your created custom post type.
When you hover the mouse on this Product tab, you will see the options: All Products to show all the posts in Product type, Add new to add a new post in Product type, etc. Those are the same with two default post types, post, and page.
So now, you have finished creating your custom post type using code only. Go next and try a plugin to do it.
Create Custom Post Type using MB Custom Post Types & Custom Taxonomies plugin
I am going to use the MB Custom Post Types & Custom Taxonomies plugin to create a new post type. This is an extension of the Meta Box Plugin. If you are not familiar with the Meta Box, read this post.
Before getting started
We need to install and activate MB Custom Post Types & Custom Taxonomies. It's free and you can download them directly from wordpress.org. Just go to your Dashboard > Plugins > Add New and search for "mb custom post types".
After installing and activating these above two plugins, a new menu will appear in the left panel. Select the Post Types submenu of that.
Create a new post type
Click the New Post Type button.
You'll see a board of information will appear. Fill in the singular, plural name, and the slug is auto-generated (you can change the slug as you want).
I’ve made a new post type with the name is Featured Products, the slug is featured-product. Pay attention that this slug will be used later to get the data in the front-end.
Next, you can modify the label of your post type in the Label tab if you want. All the options, just like the slug, are auto-created based on your post type's name you've just entered.
If you want to add more advanced information for your post type, press the Advanced tab then a new board with dozens of fields appears to edit.
Here are some options you may need:
Public queryable: choose it to allow getting the taxonomy’s data and display it on the website
Hierarchical: choose it to make the post type hierarchical - it means you can have a parent post type and its sub-post types inside
Show UI: tick to show the post type as the menu in the left menu in the Dashboard
Show in menu: tick to show the post type here:
Show in nav menu: tick to show the post type here
REST API base slug: enter REST API base slug to get the data of the post type via API if you need
Look at the Supports area. Choose the features which you want the post type to have. This part is the same as the step of declaring the $supports parameter when you create post types using the code.
In the Taxonomies tab, choose the taxonomies for this post type. If you create any custom post types, they will also appear here for you to select.
Bonus: We have a tutorial for creating custom taxonomies here.
Step 3: Save and check
When you finished setting up your custom post type, press the Publish button. Then, click the Get PHP code and the code of this post type appear immediately right below there. That is the automatically generated code for your custom post type. It is the same with the code when you did it manually.
Note that you can put the code in the functions.php file and then deactivate the MB Custom Post Type plugin. That means you can use it just like a code generator. Deactivating it might help your website runs a little bit faster.
And now, you can see the result in the admin area:
A new menu named Featured Products is now showing in the admin menu.
Is it easy and work for you? With the second method, it may be much easier, especially if you are not a developer.
Bonus: Recently, we launched a free tool called Custom Post Type Generator. It helps you generate code to register custom post types in WordPress so much more quickly and easily.
Final words
I hope that the above two methods will be useful for you when you want to create any custom post type. The code may be the barrier to contain you if you are not tech-savvy and take more time even you are a developer. So, give plugins a try. Save your time, save your efforts, and be free!
In case you need more instruction or just want to share another way to create custom post types, leave us some comments. Enjoy it!
WordPress users must be familiar with the concept of content type classification using Categories or Tags. These things also are called Taxonomy. So what is Taxonomy? Simply, it gives you various options for classifying new content types. This article will introduce you to the process of creating a custom taxonomy that stores additional custom content types without mixing them with Categories or Tags.