WordPress 3.x comes with the ability to create Custom Post Types and which makes it very simple for developers to extend WordPress and use it as a proper CMS for any business need. Apart from the default Posts and Pages, you can now create custom types to better manage content, and make it easier for your clients to update the website.
Custom Post Types can be created manually or through a plugin. it’s easier to use Plugin to accomplish this task in WordPress. There are two very good plugins for creating Custom Post Types, one is Custom Post Type UI and other is More Types. Both are very mature and useful plugins.
Of the two, More Types plugin offers more options and its authors have released two more plugins More Fields and More Taxonomies. More RTypes plugin also allow you to add all boxes such as ‘All is One SEO Pack’, ‘File Gallery’ etc for your custom post type content, which is not possible with Custom Post Type UI.
Since you need to use More Fields plugin with Custom Post Type UI plugin in any case, it;s better that you choose ‘More Types’ with ‘More Fields’. Both these plugins work wonderfully and seamlessly and if you also use More Taxonomies, it will enhance your Custom Post Type Content.
More Fields adds boxes to the Write/Edit page. These boxes contains input fields, so that additional (more) fields can be added to a post. For example, if you write about books, you can add a box where you can enter title and author, etc. The boxes can be placed either to the right or to the left on the Write/Edit page.
More Types adds new post types to the WordPress admin. For instance, if you run a celebrity website you could create a Celebrity post type (based on the post). If you run a food blog you could create a post type for recipes.
If you use More Fields in addition to More Types you could for instance add an input field where you put the ingredients and another where you input cooking time or add Actors and directors to your Movies Post type.
Displaying the More Fields Content on Front end
You can easily display the more fields contents on front-end template using following code with the “More Fields” plug-in:
meta('yourKeyHere');
Here is another better way to do the same thing:
echo get_post_meta($post->ID, 'yourKeyHere', $single = true);
Since this is standard to WordPress and its custom fields, it will work regardless of “More Fields” plugin is installed or not, your fields will display in the theme in any case so choose the second option.
Setting up Template Files
Single template
In the form of the single-type-template. In the same way that posts are shown on their own page with single.php, custom post types will use single-{posttype}.php if it’s available.
So for the above example, you could create a single-celebrity.php file and the celebrity posts would be shown using that template.
Archive Template
In the form of the archive-type-template. In the same way that posts are shown on their own archive with archive.php, custom post types will use archive-{posttype}.php if it’s available.
So for the above example, you could create a archive-celebrity.php file and the product posts would be shown using that template.
Listing Custom Post Type Records
Now you have already created the custom post type record template, you can also display listing of custom post types by using Custom Page Template. This is also a straight forward process.
Firstly, go to your theme folder and copy page.php and create a new file page-{posttype}.php, in our example case here, this will becomes page-celebrity.php.
In your newly created page-celebrity.php, add the following line somewhere in the comment at the top of the file:
/*
Template Name: Celebrity Listing Template
*/
In the Celebrity Listing template, find the following loop:
if ( have_posts() ) while ( have_posts() ) : the_post();
Add the following line above the loop:
query_posts(array('post_type'=>'celebrity')); //replace 'celebrity' with your custom post type
The Above code will instruct WordPress to find all posts that have the ‘celebrity’ type and loop through them.
Now you have created your Custom Page Template to list celebrities, but you also need to to create “Celebrities” page that will show listing of celebrity type posts using this template you just created. n order to create Page, log in to the WordPress admin panel, create a new page with the title of “Celebrities”, and then select the “Celebrity Listing Template” for the Template page attribute (see screenshot below).
Just publish this Celebrities page and add this to your navigation and you will have all your Custom Posts type posts listed on this page. You might need to add some css styles to make it look more like listing.
Hope that helps.
Cheers!