As I use WordPress more and more on a professional basis I am discovering things that make using it as a CMS more and more useful. Over the past couple of years I’ve had quite a few projects based off of WordPress and one of the things that makes me cringe the most is seeing a page that is made up of multiple parts or content types. The classic example of this is a homepage which has some sort of slider on it. The slider invariably needs to be content managed, and quite often so does the text from within it.
I have always solved this issue previously by simply creating a slide template and creating pages which are assigned to this template, usually as child pages of the homepage. A page has a few common parts, title, body copy, featured image which can all be used to create the content. Then if there are other structured requirements you can use custom fields on a post to add these elements. Overall the standard page functionality can be manipulated to achieve the desired result. This approach works, to some degree but it isn’t user friendly and can frankly be a pain to use.
Another way of doing this is to find a plug-in which adds slide functionality, and there are a large amount of plug-ins of varying quality which do this. I hate plugins in general, the quality of them is outside of your control, they can break or hinder the upgrade path unless actively developed and often they add bloat to your theme. Sure they are much more user friendly but as a plug-in is often a generalisation of the problem rather than a specific answer to a problem a plug-in doesn’t always fulfill your requirements.
The third and final way is basically a mash of the two, custom post types. A custom post type is often basically what a plug-in is. However as you are defining it you get to control it. Where you are using custom fields you get to layout the edit screen in exactly the way you want to, label the custom fields appropriately and make the creation and updating of the content much more intuitive and user friendly.
There are 2 things you need to do to get custom post types working:
1) Register the post type
register_post_type( 'home_slide', array( 'labels' => array( 'name' => __( 'Home Slides' ), 'singular_name' => __( 'Home Slide' ), 'add_new_item' => __( 'Add New Home Slide' ), 'edit_item' => __( 'Edit Home Slide' ), ), 'public' => true, 'show_in_menu' => true, 'supports' => array( 'title','editor','thumbnail' ), 'rewrite' => array( 'slug' => 'slide' ), 'exclude_from_search' => true ) );
2) Create the code to define the post type. I won’t go into detail on this one but I found the following tutorial useful to get me started:
From now on I will be using these on a lot more word press projects.