How to create Custom Post types in WordPress

Do you want better
IT Services ?

Let our experts help you navigate the digital world with tailor-made IT Solutions!

Adding a custom post type in wordpress is an easy task. You can learn a lot at at WordPress Codex about Post types.WordPress can hold and display many different types of content. A single item of such a content is generally called a post, although post is also a specific post type. Internally, all the post types are stored in the same place, in the wp_posts database table, but are differentiated by a column called post_type.

Minimum Code required to create a custom post type is following. This basic code has been taken from wordpress codex and this code will create a “Product” custom post type with minimum options. However below the basic code we have added advanced snippet with more arguments.


add_action( 'init', 'create_post_type' );
function create_post_type() {
	register_post_type( 'acme_product',
		array(
			'labels' => array(
				'name' => __( 'Products' ),
				'singular_name' => __( 'Product' )
			),
		'public' => true,
		'has_archive' => true,
		)
	);
}

Advanced Snippet to add Custom Post types in WordPress. One just need to add the following code in functions.php file to make a Portfolio custom post type. If you want a custom post type other then Portfolio, you can just replace word Portfolio with your Post Type name such as Event or Product. If you want to add more then one custom post type don’t forget to change function name everytime you create a custom post type. Make sure you write this code before php closing tag in functions.php

[box style=”green”]If you face a blank screen after adding this code,  and you can no longer access wordpress, you have played wrong with php syntax. That means you have opened <?php tag again before closing previous one. [/box]

 

If you face any problem while using this code, just write in a comment below.


if ( ! function_exists('portfolio_custom_post_type') ) {

// Register Custom Post Type
function portfolio_custom_post_type() {

$labels = array(
	'name'                => _x( 'Portfolios', 'Post Type General Name', 'text_domain' ),
	'singular_name'       => _x( 'Portfolio', 'Post Type Singular Name', 'text_domain' ),
	'menu_name'           => __( 'Portfolio', 'text_domain' ),
	'parent_item_colon'   => __( 'Parent Item:', 'text_domain' ),
	'all_items'           => __( 'All Items', 'text_domain' ),
	'view_item'           => __( 'View Item', 'text_domain' ),
	'add_new_item'        => __( 'Add New Item', 'text_domain' ),
	'add_new'             => __( 'Add New', 'text_domain' ),
	'edit_item'           => __( 'Edit Item', 'text_domain' ),
	'update_item'         => __( 'Update Item', 'text_domain' ),
	'search_items'        => __( 'Search Item', 'text_domain' ),
	'not_found'           => __( 'Not found', 'text_domain' ),
	'not_found_in_trash'  => __( 'Not found in Trash', 'text_domain' ),
	);
	$args = array(
	'label'               => __( 'portfolio', 'text_domain' ),
	'description'         => __( 'Post Type Description', 'text_domain' ),
	'labels'              => $labels,
	'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'trackbacks', 'revisions', 'custom-fields', 'page-attributes', 'post-formats', ),
	'taxonomies'          => array( 'category', 'post_tag' ),
	'hierarchical'        => false,
	'public'              => true,
	'show_ui'             => true,
	'show_in_menu'        => true,
	'show_in_nav_menus'   => true,
	'show_in_admin_bar'   => true,
	'menu_position'       => 5,
	'can_export'          => true,
	'has_archive'         => true,
	'exclude_from_search' => false,
	'publicly_queryable'  => true,
	'capability_type'     => 'page',
	);
	register_post_type( 'portfolio', $args );

}

// Hook into the 'init' action
add_action( 'init', 'portfolio_custom_post_type', 0 );

}

===========================================================

Adding Custom Post type using snippet file

  1. Click below button to download Custom Post type Snippet.
  2. Extract the zip file and add php file into your theme root folder
  3. Open functions.php and add the following code after php opening tag.

Following snippet file method for adding custom post types to your wordpress theme avoid’s any kind of silly coding errors.

require_once ('custom-post-type.php');

[button color=”#333333″ link=https://thinkncode.com/wp-content/uploads/2014/07/custom-post-type.zip target=”_self”]Download [/button]

 

Scroll to Top