In this post today I will disuse How to Create WordPress Custom Post Types and displaying with Shortcode and page template. Each new developer need to learn how to easily create custom post types in WordPress site. Also WordPress regular blog posts use Categories and Tags to create an organization structure. In this article I also disuse how to crate separate Categories and Tags for custom post types.
Working With Custom Post Types
So your new custom post types need to effectively create and use, you will need to be familiar with the following:
- Creating custom Post Types
- Creating custom Taxonomies
- Creating custom Tags
Creating Custom Post Types
Let’s start and register custom post type in your theme functions.php file by method register_post_type. It will be located in public_html/wp-content/themes/YOUR_THEME_NAME/functions.php Open this file via editor and add following code to register custom post type.
For more information I will shortly describe some args details in below.
- labels The labels option should be an array defining the different labels that a custom post type can have. I have separated this out above just to make the arguments for registering a post type clearer.
- description A short explanation of our custom post type, what it does and why we are using it.
- public This option controls a bunch of things in one go. Setting this to true will set a bunch of other options (all to do with visibility) to true.
- menu_position Defines the position of the custom post type menu in the back end. Setting it to “7” places it below the “posts” menu.
- supports This option sets up the default WordPress controls that are available in the edit screen for the custom post type. By default, only the title field and editor are shown.
- has_archive If set to true, rewrite rules will be created for you, enabling a post type archive at http://yoursite.com/posttype/ (by default)
Our current custom post types Faqs associated with a custom taxonomy called genres. I also set hierarchical to false If you would like your custom post type to behave like Pages, then you can set this value to true.
Category for Custom Post Type
Now we are going to register taxonomy for custom post type named “Faq Categories” after register_post_type method in functions.php file by register_taxonomy method. This categories we only use for Faq post types.
Registering categories for custom post type is done, We added some labels and set hierarchical option to true. This enables custom “category style” to taxonomies. When we set it to false (this is the default), taxonomy will be like the default tags.
Displaying Custom Post Types by Shortcode
Now I am going to create a shortcode [faqs] to displaying custom post type post items. Please added following code in your theme functions.php in theme public_html/wp-content/themes/YOUR_THEME_NAME/functions.php.
We can pass tow parameter via shortcode “category” Example: [faqs category=”7″] (7) is category ID, Second one is “class” Example: [faqs class=”sidebar-categories”] is CSS class.
Displaying Custom Post Types in Template
Also we can listed custom post via template file, open a template file via editor from your theme public_html/wp-content/themes/YOUR_THEME_NAME/ directory. By querying the database, you can retrieve post items from a custom post type.
So in this post we are completed 3 (three) part, 1. Create Custom Post Types, 2. Categories for Custom Post Types, 3. Displaying Custom Post Items by Shortcode and Template.
Thank you for carefully read my article If you have any query please Leave your query on following comment section or contact with us. Please SHARE this post.