Customizing WordPress Using Custom Post Types

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.

register_post_type( 'car',
    array(
        'labels' => array(
            'name' => __( 'Cars' ),
            'singular_name' => __( 'Car' ),
            'add_new' => 'Add Car', 
            'add_new_item' => 'Add Car',
            'edit' => 'Edit Cars',
            'edit_item' => 'Edit Car',
            'new-item' => 'New Car',
            'view' => 'View Car',
            'view_item' => 'View Car',
            'search_items' => 'Search Cars',
            'not_found' => 'No Cars Found',
            'not_found_in_trash' => 'No Cars Found in Trash',
            'parent' => 'Parent Car'
        ),
        'description' => 'Car Post Type',
        'public' => true,
        'has_archive' => true,
        'show_ui' => true,            
        'capability_type' => 'page',
        'rewrite' => array( 'slug' => 'car', 'with_front' => false ),
        'supports' => array('title', 'editor', 'thumbnail', 'author', 'page-attributes')
    )
);

Customizing WordPress Using Custom Post Types

A quick overview of a car tells us that there are certain characteristic that can be assigned to cars. Simple examples are:

  • Number of doors
  • Body type, e.g. Saloon, hatchback, estate or 4×4
  • Color
  • Fuel Type, e.g. Diesel or Petrol
  • Engine size

These attributes or characteristics could the custom taxonomies we will be assigning to our custom post type. An example taxonomy would be the number of doors. It would look something like this:

register_taxonomy('car_doors',
    'car',
    array (
        'labels' => array (
            'name' => 'Car Doors',
            'singluar_name' => 'Car Doors',
            'search_items' => 'Search Car Doors',
            'popular_items' => 'Popular Car Doors',
            'all_items' => 'All Car Doors',
            'parent_item' => 'Parent Car Doors',
            'parent_item_colon' => 'Parent Car Doors:',
            'edit_item' => 'Edit Car Doors',
            'update_item' => 'Update Car Doors',
            'add_new_item' => 'Add New Car Doors',
            'new_item_name' => 'New Car Doors',
        ),
        'hierarchical' =>true,
        'show_ui' => true,
        'show_tagcloud' => true,
        'show_in_nav_menus' => true,
        'show_in_menu' => true,
        'rewrite' => array( 'slug' => 'doors'),
        'query_var' => 'doors',
        'public'=>true
    )
);

wp_insert_term( '3 Door', 'car_doors', array( 'description'=> '3 Doors', 'slug' => '3_door' ) );
wp_insert_term( '4 Door', 'car_doors', array( 'description'=> '4 Doors', 'slug' => '4_door' ) );
wp_insert_term( '5 Door', 'car_doors', array( 'description'=> '5 Doors', 'slug' => '5_door' ) );

Customizing WordPress Using Custom Post Types

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.

function customize() {
   //add custom post type
   //add custom taxonomy
}
add_action( 'init', 'customize' );

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.

<?php
/**
 * The Template for displaying all single car posts.
 */

get_header(); ?>

    <div id="primary" class="site-content">
        <div id="content" role="main">

            <?php while ( have_posts() ) : the_post(); ?>

                <?php get_template_part( 'content', get_post_format() ); ?>

                <?php comments_template( '', true ); ?>

            <?php endwhile; // end of the loop. ?>

        </div><!-- #content -->
    </div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

And there you have it, a quick and easy way to customize WordPress in an easily maintainable way.

Скачать исходники: http://chilp.it/41d1db