Customizing WordPress Using Custom Post Types

4

One of WordPress’ greatest features is its ability for customization. There are many sites out there that could benefit greatly from these features. I’ve seen sites that sell cars and houses to simple music sites that could benefit from one specific customization function within WordPress, the register_post_type. The register_post_type function, as its name suggests allow the user (or theme) to add a new post type such as car, house or album. Once this post type has been registered, the user can then start adding functionality to WordPress that is unique to the post type. Using a custom post type opens up the use of custom taxonomies and more

Adding a custom post type

For this example I’m going to stick with the Car sales example. To add our post type we can define several labels that are used to populate menus and various options throughout the site. We also have to define some basic characteristics of our post type that we want WordPress to know about and handle for us. These include if we want to have an archive listing or if the post supports certain features such as excerpts.

To actually add this to an existing theme you’ll need to hook into the init action. Simply wrap the above code in a function called ‘customize’ and use the following code to call it. This is how it is configured in the attached sample code available at the bottom of this post.

Displaying a car

Now that we have our custom post type setup, next we’ll want to actually display it is a custom way. To do this we take advantage of the template hierarchy built in to WordPress. By creating a file called single-car.php (you can just copy and rename single.php from the “Twenty Twelve” theme), you can control exactly how a car post renders.