Users can add more types of content to your website than just posts and pages by using custom post types. They convert your WordPress website into a strong content management system from a blogging site (CMS).
What Does WordPress' Custom Post Type Mean?
Post types are utilized on your WordPress blog to support the identification between various content formats in WordPress. Pages and posts are both post types, although they are designed to fulfil different functions.
WordPress by default offers the following post types:
- Post
- Page
- Attachment
- Revision
- Menu Nav
Furthermore, users can develop their own post types, or custom post types. They are helpful when producing content that isn't in the usual post or page format.
For example, when you manage a website where movie reviews are posted, users are likely to want to build a post type for movie reviews. For items, reviews, and portfolios, you may also make unique post types.
Several custom fields and unique custom category structures are available for custom post types.
Custom post types are a common way for popular WordPress plugins to store information on your WordPress website. Top plugins that take advantage of special post types include the following:
- Your online store now has a "product" post type, thanks to WooCommerce.
- To save all of your forms, WPForms creates a post type called "Wpforms."
- MemberPress adds a new custom post type called "memberpress product."
Do I have to build custom post types?
It's crucial to assess your requirements before you start building custom post types for your WordPress website. A regular post or page can frequently provide the same outcomes.
So let's look at how simple it is to create new post types in WordPress to suit your needs.
We'll show two approaches as well as a few other ways to show custom post types on your WordPress website.
- With WPCode, building a custom post type is recommended.
- Using a Plugin to Create a Custom Post Type
- Using Custom Post Types to Show on Your Site
Manually Developing a New Post Type With WPCode:
You must add code to your theme's functions.php file in order to create a new post type. Generally, we wouldn't advise this to anyone but really regular users because even a small error could completely wreck your website. The code would be removed if you updated your theme.
To add code to your WordPress website, WPCode is the quickest and safest method for anyone to do so.
With WPCode, you may replace numerous specialised or one-use plugins you may have installed by adding custom snippets and activating a number of features from its predecessor, the code library.
Installing and activating the free WPCode plugin is the first step:
Go to Code Snippets » Add Snippet to your WordPress admin area once it has been activated. Next, select "Use Snippet" after moving your mouse over "Add Your Own Code (New Snippet).
The "Create Custom Snippet" screen will then appear.
Giving your code snippet a name and setting the switch to "Active" are now options.
After that, simply paste the following code into the "Code Preview" field. Any theme may be used with this code to create a basic post type named "Movies," which will show up in your admin sidebar.
1. // Our custom post type function
2. function create_posttype() {
3.
4. register_post_type( 'movies',
5. // CPT Options
6. array(
7. 'labels' => array(
8. 'name' => __( 'Movies' ),
9. 'singular_name' => __( 'Movie' )
10. ),
11. 'public' => true,
12. 'has_archive' => true,
13. 'rewrite' => array('slug' => 'movies'),
14. 'show_in_rest' => true,
15.
16. )
17. );
18. }
19. // Hooking up our function to theme setup
20. add_action( 'init', 'create_posttype' );
Easily replace your own CPT slug and title for movies and Movies if you only want a simple custom post type, next select the "Update" button.
Use the following code in place of the one above, though, if you want even more modification possibilities for your custom post type.
The code following gives the "Movies" custom post type many more features, such as support for revisions, featured photos, custom fields, and affiliation with a "genres" custom taxonomy.
Remember: Because both of these snippets create the same custom post type, combining them will cause WordPress to give you an error.
If you just want a basic custom post type, then just replace movies and movies with your own CPT slug and name and click the ‘Update’ button.
Use the following code in place of the one above, though, if you want even more customization possibilities for your custom post type.
The code following gives the "movies" custom post type many more features, such as support for revisions, featured photos, custom fields, and affiliation with a "genres" custom taxonomy.
Remember: These two snippets register the same custom post type; therefore, combining them will result in WordPress throwing an error. For any new post type you want to add, we advise using WPCode to create a brand-new snippet.
1. /*
2. Creating a function to create our CPT
3. */
4.
5. function custom_post_type()
6.
7. // Set UI labels for Custom Post Type
8. $labels = array
9. 'name' => _x( 'Movies', 'Post Type General Name', "twenty Twenty One"),
10. 'singular_name' => _x('Movie', 'Post Type Singular Name', 'twentytwentyone');
11. 'menu_name' => __('Movies', 'twentytwentyone')
12. 'parent_item_colon' => __('Parent Movie', 'twentytwentyone'),
13. 'all_items' => __('All Movies', 'twentytwentyone'),
14. 'view_item' => __('View Movie', 'twentytwentyone'),
15. 'add_new_item' => __('Add New Movie', 'twentytwentyone'),
16. 'add_new' => __('Add New', 'twentytwentyone'),
17. 'edit_item' => __('Edit Movie', 'twentytwentyone'),
18. 'update_item' => __('Update Movie', 'twentytwentyone'),
19. 'search_items' => __('Search Movie', 'twentytwentyone'),
20. 'not_found' => __('Not Found', 'twentytwentyone'),
21. 'not_found_in_trash' = __ ('Not found in Trash', 'twentytwentyone'),
22. );
23.
24. // Set other options for Custom Post Type
25.
26. $args = array
27. 'label' => __('movies', 'twentytwentyone'),
28. 'description' => __('Movie news and reviews', 'twentytwentyone'),
29. 'labels' = $labels,
30. // Features this CPT supports in the Post Editor
31. 'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields',"
32. You can associate this CPT with a taxonomy or custom taxonomy.
33. 'taxonomies' = array('genres')),
34. A hierarchical CPT is like Pages and can have
35. Parent and child items A non-hierarchical CPT
36. is like posting.
37. */
38. 'hierarchical' = false
39. 'public' = true,
40. 'show_ui' = true,
41. 'show_in_menu' => true,
42. 'show_in_nav_menus' => true,
43. 'show_in_admin_bar' => true,
44. 'menu_position' = 5,
45. 'can_export' => true,
46. 'has_archive' => true,
47. 'exclude_from_search' => false,
48. 'publicly_queryable' = true,
49. 'capability_type' = 'post',
50. 'show_in_rest' => true,
51.
52. );
53.
54. // Registering your Custom Post Type
55. register_post_type('movies', $args);
56.
57. }
58.
59. /* Hook into the "init" action so that the function
60. Containing our post type registration is not
61. unnecessarily executed.
62. */
63.
64. add_action('init', 'custom_post_type', 0);
Users may have also noticed that we set the organisational value to false in that section. Users can set this value to true if they really want their custom post type to look like pages rather than posts.
Another thing to note is the string "20:00:1" being used repeatedly; this is known as the text domain. Users must specify the text domain used by your theme when it's translation-ready and you'd like your post types to be translated.
The style.css file in your direction or by heading to Appearance » Theme File Editor in your admin side are both good places to look for your theme's text domain. The file head will make note of the text domain.
Simply swap out 20.1 for the text domain of your chosen theme.
After you're satisfied with the modifications, just select "Update," and WPCode will take care of the rest.
Using a Plugin to Create a Custom Post Type:
Using a plugin is another simple method for developing a custom post type in WordPress. As it is secure and incredibly simple, this method is advised for beginners.
The Custom Post Type UI plugin needs to be installed and activated first.
To set up a fresh custom post type after activation, go to CPT UI » Add or Modify Post Types. The 'Add New Post Type' tab should be selected.
Users must first give your custom post type a slug, such as "movies." Only letters and digits are allowed in this URL because it will be used in both the URL and WordPress questions. Users must then list the plurality and single names for your custom post type below.
Users can then choose to select the link that reads "Populate more labels based on specified labels" when they'd like. You'll typically save time by having this automatically fill all of the extra label fields below.
Scroll down to the area labelled "Additional Labels" now. Users will now need to provide an explanation for their post type and modify labels if they neglected to follow the above option.
While you are handling material for that specific post type in the WordPress user interface, these labels will be used everywhere.
The post type settings are the next. From this point, users can configure various post type attributes. There is a brief description of what each option does with it.
For example, users can decide whether to invert the regular pattern of posts or make a post type not hierarchical like pages.
The option to choose which editing functionality this post type would enable is located below the general settings. Simply check the boxes next to the choices the users would like to present.
Finally, select the "Add Post Type" option to save and build your own post type.
Users can now add content because you have successfully built your custom post type.
Showing custom post types on your site:
Displaying your own post types is built into WordPress. It's time to display your new custom post type's contents on your website once you've added a few things to it.
You can employ a few different techniques, and each one has its own advantages.
Custom Post Types Displayed Using the Default Archive Template
You can add a custom link to your menu by first going to Appearance » Menus. The link to your custom post type is contained in this custom link.
Your custom post type's URL will probably resemble this when you're using SEO-friendly permalinks:
Your custom post type URL will look like this when you aren't using SEO-friendly permalinks:
http://example.com/?post type=movies
Remember to change "example.com" to your own domain name and "movies" to the name of your custom post type.
Access the front page of your site after saving your menu. If you click on the recently founded menu item, your custom post type's archive page will be displayed using the archive.php template file.
Creating Templates for Custom Post Types:
Choose a template specifically designed for custom post type archives when users don't like how your custom post type's archive page looks.
All you have to do is make a new file called archive-movies.php in your theme directory. Be sure to substitute the name of your custom post type for "movies."
Copy the contents of your theme's archive.php file into the archive-movies.php template to get started, then edit it as necessary to suit your needs.
This design will now be applied to display your custom post type's archive page anytime it is accessed.
A standard template can also be used for the single entry displayed for your post type.You must create single-movies.php in the direction given to accomplish that. Remember to substitute the title of your custom post type for "movies."
Copy the single.php template's content into the single-movies.php theme to get started, and afterwards, start modifying it to suit your needs.
Custom Post Types Displayed on the Home Page:
Utilising custom post types has the benefit of keeping your custom content types apart from your standard posts. Users can, however, choose to display custom post types on your website's front page.
Simply use the free WPCode plugin to add this code as a new snippet.
1. add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
2.
3. function add_my_post_types_to_query($query)
4. If (is_home() and $query->is_main_query()
5. $query-> set("post_type," "array," "post," "movies")
6. return $query;
7. }
Remember to replace your custom post type with "movies.
Custom Post Types Question:
This is a way to execute loop questions in your themes when you're experienced with programming and would like to do so. You can obtain items from a custom post type by doing a database query.
To show the custom post type, you must paste the following snippet of code into the template.
1. <?php
2. $args = array( 'post_type' => 'movies', 'posts_per_page' => 10);
3. $the_query = new WP_Query($args);
4. ?>
5. If ($the_query->have_posts()):?php
6. ?php while ($the_query->have_posts()): $the_query->the_post();?>
7. h2>?php the_title();?>/h2>
8. <div class="entry-content">
9. ?php the_content();?>
10. </div>
11. <?php endwhile;
12. wp_reset_postdata(); ?>
13. <?php else: ?>
14. p>?php _e('Sorry, no posts matched your criteria.');?>/p>
15. ?php endif;?>
In the parameters for our new WP_Query class, this code determines the post type and the number of posts per page. The question is then done, the posts are obtained, and they are shown inside the loop.
Custom Post Types Displayed in Widgets
You'll see that WordPress comes with a standard widget to show the latest posts, but it does not provide you the option to select a specific post type.
What about if you want to use a widget to show the most recent posts from your recently created post type? The process for doing this is simple.
Installing and activating the Custom Post Type Plugins plugin is the first thing users should do.
To add the "Recent Posts (Custom Post Type)" widget to a sidebar after activation, go to Appearance » Widgets.
Users may use this widget to display the latest blog post from every post type. Users must choose the items you want and your custom post type from the "Post Type" selection.
Then, go to your site to view the widget in action, and be sure to click the 'Update' button in the upper right corner of the screen.
Furthermore, the plugin has widgets for specific post types that show archives, a calendar, categories, the most recent comments, search, and a tag cloud.
Add your Comment