- Home
- About us
- Services
- Hire Developer
- Industry
- Portfolio
- Testimonials
- White Label Services
- Blog
- Contact
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.
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.
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.
Let’s walk through both methods.
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?
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.
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.
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:
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
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 2024 F5 Buddy Pvt. Ltd.. All Rights Reserved