Custom post type


Right now, i will try to show how to make custom post type in WordPress. With custom type post you can built a post with anything field as you like. And you can create taxonomy like category in post. I will give example “dealer” custom type post. Dealer custom type post have 3 field (address,tel,and fax) with one taxonomy (regions). OK let start.
For the first, we must register custom post type.

add_action('init', 'dealer_register');

function dealer_register() {

$labels = array(
‘name’ => _x(‘Dealer’, ‘post type general name’),
‘singular_name’ => _x(‘Dealer Item’, ‘post type singular name’),
‘add_new’ => _x(‘Add New’, ‘dealer item’),
‘add_new_item’ => __(‘Add New Dealer’),
‘edit_item’ => __(‘Edit Dealer’),
‘new_item’ => __(‘New Dealer’),
‘view_item’ => __(‘View Dealer’),
‘search_items’ => __(‘Search Dealer’),
‘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() . ‘/_img/article16.png’,
‘rewrite’ => true,
‘capability_type’ => ‘post’,
‘_builtin’ => false,
‘hierarchical’ => false,
‘menu_position’ => null,
‘supports’ => array(‘title’,’thumbnail’)
);

register_post_type( ‘dealer’ , $args );
}

After we register custom type post, we can create the taxonomy. please see this code

register_taxonomy("regions", array("dealer"), array("hierarchical" => true, "label" => "Regions", "singular_label" => "Regions", "rewrite" => true));

For the second step i will give the example for create the post meta. please see this code

add_action("admin_init", "admin_init");

function admin_init(){
add_meta_box(“dealers_meta”, “Dealer Details”, “dealers_meta”, “dealer”, “normal”, “low”);
}

function dealers_meta() {
global $post;
$custom = get_post_custom($post->ID);
$address = $custom[“address”][0];
$tel = $custom[“tel”][0];
$fax = $custom[“fax”][0];
?>
<p><label>Address:</label><br />
<textarea cols=”50″ rows=”5″ name=”address”><?php echo $address; ?></textarea></p>
<p><label>Tel:</label><br />
<input type=”text” name=”tel” value=”<?php echo $tel; ?>” />
</p>
<p><label>Fax:</label><br />
<input type=”text” name=”fax” value=”<?php echo $fax; ?>” />
</p>
<?php
}
add_action(‘save_post’, ‘save_details’);
function save_details(){
global $post;

update_post_meta($post->ID, “address”, $_POST[“address”]);
update_post_meta($post->ID, “tel”, $_POST[“tel”]);
update_post_meta($post->ID, “fax”, $_POST[“fax”]);
update_post_meta($post->ID, “type”, $_POST[“type”]);
}

And then i will give the example for make a column list of dealers

add_action("manage_posts_custom_column", "dealer_custom_columns");
add_filter("manage_edit-dealer_columns", "dealer_edit_columns");

function dealer_edit_columns($columns){
$columns = array(
“cb” => “<input type=\”checkbox\” />”,
“title” => “Dealer Name”,
“address” => “Address”,
“tel” => “Tel”,
“fax” => “Fax”,
“regions” => “Regions”,
);

return $columns;
}
function dealer_custom_columns($column){
global $post;

switch ($column) {
case “address”:
$custom = get_post_custom();
echo $custom[“address”][0];
break;
case “tel”:
$custom = get_post_custom();
echo $custom[“tel”][0];
break;
case “fax”:
$custom = get_post_custom();
echo $custom[“fax”][0];
break;
case “regions”:
echo get_the_term_list($post->ID, ‘regions’, ”, ‘, ‘,”);
break;
}
}

Then finish. You can copy all of code at functions.php. Please try that’s code.
And don’t forget to leave the comment and share this post in your social network.
Keep posting all…thx

Advertisements

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