Create Custom Post Type with category

A custom post type can be added to WordPress via the register_post_type() function. This function allows you to define a new post type by its labels, supported features, availability and other specifics.

add_action('init','postype');
function postype() {
$labels = array(
'name' => _x('Postype', 'post type general name'),
'singular_name' => _x('Postype', 'post type singular name'),
'add_new' => _x('Add New Postype', 'New Postype'),
'add_new_item' => __('Add New Postype'),
'edit_item' => __('Edit Postype'),
'new_item' => __('New Postype'),
'view_item' => __('View Postype'),
'search_items' => __('Search Postype'),
'not_found' =>  __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);

$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'menu_icon' => get_stylesheet_directory_uri() . '/article16.png',
'rewrite' => array('slug' => 'postype'),
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','editor','page-attributes','thumbnail','custom-fields','excerpt','comments'),
'taxonomies' => array('post_tag'),
'has_archive' => true
);
register_post_type( 'postype' , $args );
}

// Creating custom Taxanomy
add_action( 'init', 'create_postype_category' );

function create_postype_category() {
$labels = array(
'name' => _x( 'Location', 'taxonomy general name' ),
'singular_name' => _x( 'Location', 'taxonomy singular name' ),
'search_items' =>  __( 'Search Location' ),
'all_items' => __( 'All Location' ),
'parent_item' => __( 'Parent Location' ),
'parent_item_colon' => __( 'Parent Location:' ),
'edit_item' => __( 'Edit Location' ),
'update_item' => __( 'Update Location' ),
'add_new_item' => __( 'Add New Location' ),
'new_item_name' => __( 'New Location' ),
);

register_taxonomy('postype-category','postype',array(
'hierarchical'  => true,
'show_ui'         => true,
'labels'         => $labels
));
}

Advertisements

12 thoughts on “Create Custom Post Type with category

  1. bbom

    what a great article that is really informative and innovative informed with new updates. its was really valuable. thank you very much.

  2. Andrew

    hi, thanks for your article!

    would you be so kind to answer to some questions? I’m a newbie in wordpress world…

    – first of all in which file I have to put those functions?
    – I guess that code has to be run only once, correct?
    – can I create as many categories as I want?
    – how can I add translations (multi-language strings) to your code, instead of plain English?
    – if I’d make some mistakes, can I simply delete the new post type and categories within it?

    Thanks in advance.

  3. futbol en directo

    Hello to every body, it’s my first go to see of this weblog; this web site contains remarkable and in
    fact excellent material in favor of visitors.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s