08Jun

All about Custom Post Types – WordPress Theme Development

WordPress has widely extended its functionality to provide its users with a full-fledged content management system than just a blogging platform. One of the exciting elements of WordPress theme development is Custom Post Types. 

Earlier, WordPress was limited to some built-in post types or content types like – posts, pages, and media. That is, you can add evergreen content and pages like About and Contact, and publish entries like company news and blog posts. 

Now, WordPress also supports Custom Post Types. Here’s how you can leverage them.

What are Custom Post Types, and why do you need them? 

Custom Post Types are specific post types to add content that does not fall into the category of built-in post types in WordPress. Using it, you can embed a new post type to your website with supported features, availability, and labels. You will be able to work with this custom post type as you do with posts and pages

So, if you want to take WordPress website development to the next level, with a unique content type (specific to your need), Custom Post Types will serve the purpose. If there’s something different from conventional posts and pages to publish, organize your website content well and go for custom post types. 

How to create Custom Post Types in WordPress?

There are two ways to do this – and if you don’t want to do it yourself, you can hire an expert WordPress developer, the third option available. 

  1. Create custom post types using a plugin
  2. Create custom post types manually

Let’s walk through both methods. 

Creating custom post types – Use a plugin

If you’re a beginner and want to explore the flexibility of WordPress post types, then you can go with plugins. It is an easy and safe method. 

How can you do this?

  1. Install and activate the plugin – Custom Post Type UI.
  2. After activating, you will see a new menu item in your WordPress admin menu – it’s called CPT UI. 
  3. Click on CPT UI and select Add New for creating a custom post type.
  4. Provide a slug for the custom post type to be used in URL and WordPress queries (make sure it contains letters and numbers).
  5. Add plural and singular names of your custom post type.
  6. Click on the link in front of Auto-populate Labels to fill the rest of the label fields.
  7. Go to Additional Labels and provide your post type description and change labels. 
  8. Scroll down to Settings to manage different attributes for your post type. 
  9. Apart from the general settings, you have the option to select which editing features are allowed in this post type. 
  10. Finally, save the changes by clicking Add Post Type.

Creating Custom Types – Manual Approach

Plugins can sometimes go tricky. When your plugin is deactivated, your data remains, but the custom post types will disappear. You won’t find them in the admin area as it gets unregistered. 

Therefore, when working on a client site and don’t want to install another plugin, you can add the required code to create a custom post type manually. 

  • Go to your theme’s function.php file to add the code is register_post_type function. 
  • You can add many more options to your code to add the desired functionality to your custom post type. 

If you want to be the best WordPress Developer for your WordPress site, you must learn to embed the manual code right. First, we will show you a quick and fully working example so that you understand how it works. Take a look at this code:

// Our custom post type function
function create_posttype() {

register_post_type( ‘movies’,
// CPT Options
array(
‘labels’ => array(
‘name’ => __( ‘Movies’ ),
‘singular_name’ => __( ‘Movie’ )
),
‘public’ => true,
‘has_archive’ => true,
‘rewrite’ => array(‘slug’ => ‘movies’),
‘show_in_rest’ => true,

)
);
}
// Hooking up our function to theme setup
add_action( ‘init’, ‘create_posttype’ );

What this code does is that it registers a post type 'movies' with an array of arguments. These arguments are the options of our custom post type.

How to display custom post types on your site?

Now, when your custom post type is ready, it’s time to go onboard. WordPress provides built-in support to display custom post types. Make sure you’re ready with your data to be published on the site. Again, there are a couple of methods you can use to do this:

Displaying Custom Post Types on The Front Page


add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'movies' ) );
return $query;
}

Replace the post type “movies” With Your Own

Using the Default Archive Template

  1. Go to Appearance.
  2. Select Menus.
  3. Add a custom link to your custom post type to the menu.
  4. Save your menu. 
  5. Go to your site’s front-end, and you’ll see a new menu added on your website, which directs you to the custom post type archive page. 

Use Custom Templates for CPT Archives

  1. Create a new file in your theme directory. 
  2. Name it archive-cptname.php (cptname is the name of your custom post type). 
  3. Copy the contents of the theme’s archive.php file into archive-cptname.php and modify it as per your needs. 
  4. Now, whenever you access the custom post type archive page, this template will display it. 

You can also display your custom post types in Widgets

  1. Install and activate the Ultimate Post Widgets plugin. 
  2. Go to appearance and select widgets. 
  3. Drag and drop the Ultimate Post Widgets to a sidebar. 
  4. Now you can show the recent posts from any post types. 

We hope this read helps you to achieve your goal. If not, contact F5 Buddy for WordPress Theme Development. You just have to discuss your requirements and our team will get your work done. 

© Copyright 2021 F5 Buddy. All Rights Reserved