To create custom post types, using the register_post_type() function. It takes two parameters – a custom post type name and a $args array. A plugin’s code should also contain custom functions that call register_post_type(). Recall to hook these functions to the init action hook to permit the custom post type to register properly. To avoid conflict with other plugins, your custom function should have a prefix like webart_.

Here is a code example for a Custom Article post type and you need to put this code in the function.php file in wordpress theme.

<?php

// Hook webart_custom_post_custom_article() to the init action hook
add_action( 'init', 'webart_custom_post_custom_article' );
// The custom function to register a custom article post type
function webart_custom_post_custom_article() {
// Set the labels. This variable is used in the $args array
    $labels = array(
        'name'               => __( 'Custom Articles' ),
        'singular_name'      => __( 'Custom Article' ),
        'add_new'            => __( 'Add New Custom Article' ),
        'add_new_item'       => __( 'Add New Custom Article' ),
        'edit_item'          => __( 'Edit Custom Article' ),
        'new_item'           => __( 'New Custom Article' ),
        'all_items'          => __( 'All Custom Articles' ),
        'view_item'          => __( 'View Custom Article' ),
        'search_items'       => __( 'Search Custom Article' ),
        'featured_image'     => 'Poster',
        'set_featured_image' => 'Add Poster'
    );
// The arguments for our post type, to be entered as parameter 2 of register_post_type()
    $args = array(
        'labels'            => $labels,
        'description'       => 'Holds our custom article post specific data',
        'public'            => true,
        'menu_position'     => 5,
        'supports'          => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments', 'custom-fields' ),
        'has_archive'       => true,
        'show_in_admin_bar' => true,
        'show_in_nav_menus' => true,
        'query_var'         => true,
    );
    // Call the actual WordPress function
    // Parameter 1 is a name for the post type
    // Parameter 2 is the $args array
    register_post_type('article', $args);
}

Conclusion:

You may build an unlimited number of custom post types.

Similar Posts