26Jul
  • If you want to create a custom post type you need to include this code in your theme in the function.php file.
add_action( 'init', 'create_my_post_types' );

function create_my_post_types() {
	register_post_type( 'super_duper',
		array(
			'labels' => array(
				'name' => __( 'Super Dupers' ),
				'singular_name' => __( 'Super Duper' )
			),
			'public' => true,
		)
	);
}

You can customize your post type in many way.


Label

The label argument is an array of string that change the text of many part of the admin panel.

'labels' => array(
	'name' => __( 'Super Dupers' ),
	'singular_name' => __( 'Super Duper' ),
	'add_new' => __( 'Add New' ),
	'add_new_item' => __( 'Add New Super Duper' ),
	'edit' => __( 'Edit' ),
	'edit_item' => __( 'Edit Super Duper' ),
	'new_item' => __( 'New Super Duper' ),
	'view' => __( 'View Super Duper' ),
	'view_item' => __( 'View Super Duper' ),
	'search_items' => __( 'Search Super Dupers' ),
	'not_found' => __( 'No super dupers found' ),
	'not_found_in_trash' => __( 'No super dupers found in Trash' ),
	'parent' => __( 'Parent Super Duper' ),
),

Description

The description argument is useless it’s only see in the code. You can describe your post type so other coder can understand the post type. You can also comment your post type in the code.

'description' => __( 'A super duper is a type of content that is the most wonderful content in the world. There are no alternatives that match how insanely creative and beautiful it is.' ),

Public

The public argument is a kind of catchall argument for several other arguments.

  • show_ui: Whether to show the administration screens.
  • publicly_queryable: Whether queries for this post type can be performed from the front end.
  • exclude_from_search: Whether the posts should appear in search results.
<code>'public' => true,
'show_ui' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,</code>

Menu Position

The menu position argument place your post type in a specific place in the admin menu.
You can refer to this article for detail on menu_position.
Menu position in custom post type or anything you add to the wordpress menu

'menu_position' => 20,

Menu Icon

The menu icon argument change the default icon in the admin menu.

'menu_icon' => get_stylesheet_directory_uri() . '/images/super-duper.png',

Hierarchical

The hierarchical argument allows you to choose whether you want your post type to be hierarchical. It defaults to false. If you set it to true, your posts will behave like pages in WordPress.

'hierarchical' => true,

Query var

This will default to the name of your taxonomy.

'query_var' => true,

Capabilities

This is more complex. It give you the ability to change permissions to edit, create, and read your custom post type.
I will refer you to an very good article about this topic from Justin Tadlock.

Supports

The supports argument allows you to define what meta boxes and other fields will appear on the screen when editing or creating a new post.
There are several available options:

  • title: Text input field to create a post title.
  • editor: Content input box for writing.
  • comments: Ability to turn comments on/off.
  • trackbacks: Ability to turn trackbacks and pingbacks on/off.
  • revisions: Allows revisions to be made of your post.
  • author: Displays a select box for changing the post author.
  • excerpt: A textarea for writing a custom excerpt.
  • thumbnail: The thumbnail (featured image in 3.0) uploading box.
  • custom-fields: Custom fields input area.
  • page-attributes: The attributes box shown for pages. This is important for hierarchical post types, so you can select the parent post.
'supports' => array( 'title', 'editor', 'excerpt', 'custom-fields', 'thumbnail' ),

Rewrite

The rewrite argument allows you to define a custom permalink structure.
The following example will have this result. yoursite.com/cool/post-name

'rewrite' => array( 'slug' => 'cool', 'with_front' => false ),

Taxonomies

With the taxonomies argument you control your taxonomies.
This is again more complex. I will refer you to a very good article about taxonomies from …

'taxonomies' => array( 'post_tag', 'category '),

Everything in this post I learned it from a very interesting article from Justin Tadlock.

He have all the credit for this.

Leave a Comment

Current day month ye@r *

Error: Please check your entries!