Custom post type in wordpress

Created at 23-Aug-2023 , By samar

Creating a custom post type in WordPress allows you to define and manage new types of content beyond the default posts and pages. This is particularly useful when you want to organize, display, and manage specific types of content with unique attributes. Custom post types are powerful tools that help you structure your website according to your needs.

In your theme's functions.php, use the register_post_type() function to define your custom post type.

function custom_post_type_blogs() {
    $labels = array(
        'name' => 'Blogs',
        'singular_name' => 'Blog',
        'menu_name' => 'Blogs',
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'has_archive' => true,
        'menu_position' => 5,
        'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'revisions', 'custom-fields', 'post-formats'),
        'taxonomies' => array('category', 'post_tag'),
        'rewrite' => array('slug' => 'blogs'),
    );

    register_post_type('blogs', $args);
}

add_action('init', 'custom_post_type_blogs');

In this example, we're creating a custom post type called "Blogs" You can customize the labels, features, and other parameters to match your content type.

List of all supports parameter

The supports parameter in the register_post_type() function allows you to specify which features and functionality you want to enable for your custom post type. Options include 'title', 'editor', 'thumbnail', 'excerpt', 'comments', 'custom-fields', 'author', 'trackbacks', 'comments', ''revisions', 'page-attributes', 'post-formats'. Choose the ones that fit your content type.

Set Up Taxonomies

You can associate taxonomies (like categories and tags) with your custom post type to enhance content organization. Use the taxonomies parameter in the register_post_type() function to include the desired taxonomies.

Set Up Rewrite Rules

The rewrite parameter lets you define the URL structure for your custom post type. Specify a 'slug' to determine the post type's URL.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.