You are currently viewing How to add ccustome post type in wordpress

How to add ccustome post type in wordpress

Today we will learn how to add a custom post type in the WordPress backend. you can use this in your themes code or develop a plugin for this.

This can be added directly to your functions, PHP file or you can include an external file that contains this code to your functions.php

Step 1: The below code will add the Custom menu section and the editor section of it.

< ?php

add_action('init', 'portfolio_register');

function portfolio_register() {

$labels = array(
'name' => _x('Portfolio', 'post type general name'),
'singular_name' => _x('Portfolio Item', 'post type singular name'),
'add_new' => _x('Add New', 'Portfolio item'),
'add_new_item' => __('Add New Portfolio Item'),
'edit_item' => __('Edit Portfolio Products Item'),
'new_item' => __('New Portfolio Item'),
'view_item' => __('View Portfolio Item'),
'search_items' => __('Search Portfolio'),
'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() . '/includes/icon_03.png',
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','editor','thumbnail')
);

register_post_type( 'Portfolioproducts' , $args );
}

?>

step 2:The next code will add the taxonomy or category for your custom posts

< ?php register_taxonomy("Products", array("Portfolio"), array("hierarchical" => true, "label" => "Portfolio Category", "singular_label" => "Portfolio Category", "rewrite" => true)); ?>

Here is everything is complete you can use this, but the next code is for those who want to add more fields or text areas in the custom post types which you can call custom fields.

Step 3 :

< ?php add_action("admin_init", "admin_init");

function admin_init(){
add_meta_box("credits_meta", "Shipping Options & Price", "credits_meta", "Portfolio", "normal", "low");
}
function credits_meta() {
global $post;
$custom = get_post_custom($post->ID);
$shipping_quantiity = $custom["shipping_quantity"][0];
$shipping_price = $custom["shipping_price"][0];
$book_price = $custom["book_price"][0];
?>

Shipping Quantity	Shipping Price
	
Book Price: 

< ?php
}

So that’s, End of this part means everything is complete from the back end.
you can check the output of this code.

How to call this custom post type at the front end into your file

This Post Has 3 Comments

  1. mrshahrukh123

    nice post

Leave a Reply