Initializing our plugin:
<?php
/*
Plugin Name: Add Persons
Plugin URI: http://webspeaks.in/
Description: Add new Persons.
Author: Arvind Bhardwaj
Version: 1.0
Author URI: http://webspeaks.in/
*/
?>
We have just created our new plugin with name ‘Add Persons’. Plugin creation has been descibed in the previous post.
Creating New Post-Type:
WordPress provides a very powerful function for creating custom post-types that will take all headaches for us and make us free from writing any clumsy code. The function is ‘register_post_type‘
The usage goes as:
<?php register_post_type( $post_type, $args ) ?>
The function ‘register_post_type‘ takes two parameters $post_type and $args. Post tpye is the type of post that we are creating. In this example we will create post of type ‘person’.
$args is an array of arguments. Complete reference of the arguments can be found at Function Reference/register post type
Lets write the code for it:
<?php
add_action( 'init', 'create_Person' );/*Called when WrdPress initializes*/
function create_Person() {
register_post_type( 'person',
array(
'labels' => array(
'name' => __( 'Persons' ), /*general name for the post type, usually plural*/
'singular_name' => __( 'Persons' ), /*name for one object of this post type*/
'add_new' => __( 'Add Person' ), /*the add new text*/
'add_new_item' => __( 'Add New Person' ), /*the add new item text*/
'edit' => __( 'Edit' ), /*the edit text*/
'edit_item' => __( 'Edit Person' ), /*the edit text*/
'new_item' => __( 'New Person' ), /*the new item text*/
'view' => __( 'View Person' ), /*the view text*/
'view_item' => __( 'View Person' ), /*the view item text*/
'search_items' => __( 'Search Persons' ), /*the search items text*/
'not_found' => __( 'No Persons found' ), /*the not found text*/
'not_found_in_trash' => __( 'No Persons found in Trash' ), /*the not found in trash text*/
'parent' => __( 'Parent Person' ), /*the parent text*/
),
'description' => 'Add new Person to your site!' /*A short descriptive summary of what the post type is*/
'public' => true, /*Meta argument used to define default values for publicly_queriable, show_ui, show_in_nav_menus and exclude_from_search*/
'show_ui' => true, /*Whether to generate a default UI for managing this post type*/
'publicly_queryable' => true, /*Whether post_type queries can be performed from the front end*/
'exclude_from_search' => false, /*Whether to exclude posts with this post type from search results*/
'menu_position' => 5, /*below posts*/
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'comments' ), /*the meta boxes to be shown on the add post page*/
'taxonomies' => array('post_tag','category'), /*taxonomies to be used*/
)
);
}
?>
The parameters are self descriptive in themselves. Comments have been added to clarify the usage.
Accessing our custom post-type:
<?php $loop = new WP_Query( array( 'post_type' => 'person', 'posts_per_page' => 10 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php the_title( '<h2 class="entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h2>' ); ?>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php endwhile; ?>
Passing a ‘post_type’ parameter in WP_Query can make your life simpler. Now you can access you new post-types just like ordinary posts.
So thats all for our custom post-types. We will meet again for custom meta boxes!
Thanks for adding the comments to the array of labels – that’s the sort of thing that seems obvious but can be confusing. Cleared up a couple of things for me!
and everyone else does too
I delight in, lead to I found just what I was
having a look for. You have ended my four day lengthy hunt!
God Bless you man. Have a nice day. Bye
Definitely believe that that you stated. Your favorite reason seemed to be on the net the simplest thing to take into account of.
I say to you, I definitely get irked at the same
time as folks think about concerns that they plainly do not recognise about.
You controlled to hit the nail upon the highest and defined out the whole thing
without having side effect , folks can take a signal. Will
likely be again to get more. Thanks
Hello it's me, I am also visiting this web page on a regular
basis, this web page is really nice and the users are genuinely sharing pleasant
thoughts.
Good web site you have here.. It's difficult to find high-quality writing like yours
nowadays. I honestly appreciate people like you! Take care!!
Hello, i feel that i noticed you visited
my blog thus i got here to return the choose?.I am attempting to in finding issues to enhance my
web site!I assume its good enough to use some of your concepts!!
Magnificent goods from you, man. I've understand your stuff previous
to and you are just extremely excellent. I
really like what you've acquired here, certainly like
what you're saying and the way in which you say it.
You make it enjoyable and you still take care of to keep it wise.
I can't wait to read much more from you. This is really a terrific web site.
El blanco y azul celeste prevalecen en esta obra para componer
una atmosfera de gran sensibilidad.