How to add custom post type in WordPress website

0
997
how to add custom post type in wordpress

When you’re a website owner or you’re a WordPress developer. Sometimes, you need to add custom post types in your WordPress website. It’s a common problem for many WordPress. May you didn’t an idea how to add custom post type in your WordPress website. In this article, I will show how to add custom post type in WordPress website.

What is post type in WordPress?

If you want to add custom post type in your WordPress website, you have to know first about what is the post type? The post type is the individual item that, you can be stored in the database by WordPress. Suppose, when we create a page or post it’s stored on the database. So, page and post is post type. But remember that, those are independent post types. You can create any type of post type like post or page on your WordPress website.

Write code for post type in WordPress

There are many post types by default in WordPress. If you want to add any other with a custom requirement, find out functions.php file from your theme folder. It’s some risk if you’re a newbie. If you have expertise in the child theme, it’s best practice for developers. When you will be open functions.php file, you will see a lot of codes. Find out a function that is under after_setup_theme hook. If you want to add via plugin you have to find out the function which is under init hook.

Add the following codes

function your_function() {
    $labels = array(
        'name'                  => __( 'Books', 'text_domain' ),
        'singular_name'         => __( 'Book', 'text_domain' ),
        'menu_name'             => __( 'Books', 'text_domain' ),
        'name_admin_bar'        => __( 'Book', 'text_domain' ),
        'add_new'               => __( 'Add New', 'text_domain' ),
        'add_new_item'          => __( 'Add New Book', 'text_domain' ),
        'new_item'              => __( 'New Book', 'text_domain' ),
        'edit_item'             => __( 'Edit Book', 'text_domain' ),
        'view_item'             => __( 'View Book', 'text_domain' ),
        'all_items'             => __( 'All Books', 'text_domain' ),
        'search_items'          => __( 'Search Books', 'text_domain' ),
        'parent_item_colon'     => __( 'Parent Books:', 'text_domain' ),
        'not_found'             => __( 'No books found.', 'text_domain' ),
        'not_found_in_trash'    => __( 'No books found in Trash.', 'text_domain' ),
        'featured_image'        => __( 'Book Cover Image', 'text_domain' ),
        'set_featured_image'    => __( 'Set cover image', 'text_domain' ),
        'remove_featured_image' => __( 'Remove cover image', 'text_domain' ),
        'use_featured_image'    => __( 'Use as cover image', 'text_domain' ),
        'archives'              => __( 'Book archives',  'text_domain' ),
        'insert_into_item'      => __( 'Insert into book', 'text_domain' ),
        'uploaded_to_this_item' => __( 'Uploaded to this book', 'text_domain' ),
        'filter_items_list'     => __( 'Filter books list', 'text_domain' ),
        'items_list_navigation' => __( 'Books list navigation',  'text_domain' ),
        'items_list'            => __( 'Books list', 'text_domain' )
    );
 
    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'book' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
    );
 
    register_post_type( 'post_type', $args );
}
 
add_action( 'after_setup_theme', 'your_function' );

May you confused about the above codes. Don’t worry I’m describing this code. This code initiates a hook that is working to creating a custom post type in the WordPress dashboard. Now you will see a new post type in WordPress dashboard which name is “Books”. You can see this documentation in WordPress Codex.

If you see WordPress documentation, I hope you will be got a clear idea about WordPress custom post type. Because of that, It’s best practice for the developers. So, I hope you have enjoyed my article. Thanks for reading…