Using Custom Post Types & Taxonomies

WordCamp YEG 2011

Presented by Daelan Wood

Custom Post Types

Custom Post Types allow you to create customized types of content for WordPress. So rather than being stuck with the default content types (Pages & Posts) you can tailor your content types to fit your content and make things easier for the website administrators (Your Clients).

Getting Started

Implementing custom post types requires you to modify the functions.php file within your themes folder. If the theme you are using doesn't have a functions.php file you can create one and place it in the root of your themes folder.

Creating the Custom Post Type

The code snippet below will create a Custom Post Type called Job Listings. This will allow the administrators of the site to easily allow Job Listings to the website.

function create_job_listings() {
	register_post_type( 'job_listings',
		array(
			'labels' => array(
				'name' => __( 'Job Listings' ),
				'singular_name' => __( 'Job Listing' ),
				'add_new' => __( 'Add New' ),
			    'add_new_item' => __( 'Add New Job Listing' ),
			    'edit' => __( 'Edit' ),
			    'edit_item' => __( 'Edit Job Listing' ),
			    'new_item' => __( 'New Job Listing' ),
			),
		'public' => true,
        'menu_position' => 5,
        'supports' => array( 'title', 'editor' ),
        'rewrite' => array('slug' => 'job-listings')
		)
	);
}

add_action( 'init', 'create_job_listings' );

For the details on all configurable options see the WordPress Codex

Displaying Custom Post Types in your theme

For best results create a custom page template to display the custom post types. To do this you can create a copy of your page.php file and name it page-job-listings.php. In order to get WordPress to recognize it as a custom page template add the following code at the very top of the page before the get_header(); function call:

	

/**
 * Template Name: Job Listings
 *
 */

 get_header(); 
	
	

Grabbing the Custom Post Type content

The following code will create a custom loop that will grab all the custom post type content and display it on the page. Be sure to add this code outside of the main WordPress Loop.

	$args = array( 'post_type' => 'job_listings', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
	the_title();
	echo '<div class="entry-content">';
	the_excerpt();
	echo '</div>';
endwhile;
	
	

Linking to a permalink page for your Custom Post Type

By default WordPress will use the theme's single.php file to display the permalink page for your Custom Post Type. To create a permalink just add the following code within the custom loop you created.

	
	<a href="<?php the_permalink; ?>">View Details & Apply</a>
	
	

Creating a custom permalink page

You can easily create a custom permalink page for your custom post type by saving a copy of the theme's single.php file and naming it single_your_custom_post_type_name.php. So in our case we would name our file single-job_listings.php because the name of our Custom Post Type as defined in the first chunk of code that we used is 'job_listings'.

Custom Taxonomies

Taxonomies are a way to group and organize your content. By default WordPress has 2 types of Taxonomies:

  1. Categories (Hierarchal)
  2. Tags (Non Hierarchal)

To make things easier for our clients we can create custom taxonomies that fit within the context of the content we are creating with our Custom Post Types. For instance, for our Job Listings content a good way to categorize our content might be by Job Type (Full Time, Part Time, Freelance etc).

Add the code below to your functions.php file to create a Custom Taxonomy called Job Types and associate it with our 'Job Listings' custom Post Type.

	
 function job_taxonomy() {
       register_taxonomy(
        'job_type',
        'job_listings',
        array(
            'hierarchical' => true,
            'labels' => array(
				'name' => __( 'Job Types' ),
				'singular_name' => __( 'Job Type' ),
				'add_new' => __( 'Add New' ),
			    'add_new_item' => __( 'Add New Job Type' ),
			    'edit' => __( 'Edit' ),
			    'edit_item' => __( 'Edit Job Type' ),
			    'new_item' => __( 'New Job Type' )
			    
			),

            'query_var' => true,
            'rewrite' => array('slug' => 'job-type')
        )
    );
    }

    add_action( 'init', 'job_taxonomy' );
	

The line 'hierarchical' => true tells WordPress to treat this Custom Taxonomy as a hierarchical taxonomy (Like Categories). To create a non-hierarchical taxonomy (Like Tags) you would change the line to read: 'hierarchical' => false

The additional labels below can be added to customize the UI text for a non-hierarchical taxonomy

	
			    'add_or_remove_items' => __( 'Add or Remove Job Types' ),
			    'choose_from_most_used' => __( 'Choose from the most used Job Types' ),
			    'separate_items_with_commas' => __( 'Separate Job Types with commas' )
	

For the details on all configurable options for Custom Taxonomies see the WordPress Codex