With the release of Wordpress 3.0, two great ways to better organize and display content were introduced: post types and taxonomies. These two advances improve Wordpress' role as an all-around content management system, and they continue to prove that WP is not just a blog platform. When 3.1 releases with post formats, it will be imperative that you understand how to use and implement post types and taxonomies.
What We're Going to Cover
In this tutorial, I will talk you through the following:
- Overview of post types and taxonomies in Wordpress 3.0
- How to integrate post types and taxonomies into your themes
- Real-world examples of how to use post types and taxonomies in innovative ways within your projects
By the time you've completed these steps, I hope to fuel your projects by examining other awesome ways to integrate these organizational elements in WordPress.
A Quick Overview of WordPress Post Types
When you think of post types, the important word to remember is organization. The post type itself will not add really any functionality, but it allows us to better organize Wordpress content and build admin dashboards that are more specific to the type of site that we are working with.
Here are some important things to remember when working with post types:
- When you create a new post type, a new upper level nav element will appear the main left side admin menu. From there, all of the standard post and page editors are available.
- The URL string for a new post type will be : http://yoursite.com/{post-type}/{title}/.
- You can create post type archive pages just like you do with categories, and you can even create special template files by creating an archive-{post-type}.php file.
"In the same way that posts are shown on their own archive with archive.php, custom post types will use archive-{posttype}.php."
For other info on integrating post types, querying post types, and overall functions, visit the codex here.
Integrating Post Types via Functions.php
Integrating post types is simply a matter of including the register_post_type
function. Once you have included this in your functions.php file, your nav menu should go from something like this:
to this:
In order to create custom post types, open your template's functions.php
file in an editor, and place the following function within the file:
function create_post_type() { register_post_type( 'mysite_reviews', array( 'labels' => array( 'name' => __( 'Reviews' ), 'singular_name' => __( 'Review' ) ), 'public' => true, 'menu_position' => 5, 'rewrite' => array('slug' => 'reviews') ) ); } add_action( 'init', 'create_post_type' );
Broken down, this adds the function create_post_type
, and registers the post type mysite_reviews,
add_action( 'init', 'create_post_type' ); function create_post_type() { register_post_type( 'mysite_reviews',
You may wonder why I've named the post_type mysite_reviews, and not just reviews. I made the name more conspicuous in order to make sure that my post type wouldn't interfere with custom post type names from other plugins or themes.
Another friendly reminder, your custom post types must not exceed 20 characters, as that is limit of the database column.
Here is a summary of the important post type parameters I've set above:
- labels - Wordpress allows us to label everything from the post type's name to the label for adding new posts. A complete list can be found here. In the above function, I labeled the name of the post type and its singular name.
- public - If set to true
- menu_position - I set this to 5, which will place the post type directly under "Posts". The other placements are as follows: null (below Comments), 0 (below Media), 20 (below Pages), 60 (below first separator) and 100 (below second separator)
- rewrite - So that our actual term "mysite_reviews" doesn't get put in the URL, we set the slug to "reviews" which will be much better in the long run for our visitors, links, and SEO.
For More Information
A full list of parameter arguments for post types can be found here.
Displaying Post Types in Wordpress Themes
Since Wordpress post types are simply an extension of the existing classification system, displaying them in a theme is quite similar to what is already in place. There are currently three primary ways to display custom post types in your themes:
- Post Query
- Single Post Template
- Archive Template
Displaying via Post Query
To display the new post type mysite_reviews
, you will want to open up the template file that you would like to display it on (in my case, I usually create a custom home.php
for templates), and enter the following code:
$args = array( 'post_type' => 'mysite_reviews', '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;
This simply creates a new Wordpress loop that will display the title and excerpt from the 10 most recent entries in the mysite_reviews
post type.
Displaying via Single Post Template
Just as you can customize the way individual posts are displayed via a theme's single.php file, you can customize the way your individual post type entries are displayed. The easiest way to do this is to create a duplicate of your theme's single.php file and rename it to single-{posttypename}.php
. From there, you can customize that file to your specs. Using the example from above, we would need a single post template named single-mysite_reviews.php
.
Displaying by Archive Template
While this feature will not be available until WP 3.1 releases, post types can also be displayed archive-style by creating a file in your theme named archive-{posttypename}.php. So, if we were creating an archive for the post type, we would create an archive template named archive-mysite_reviews.php
and place it within our template folder.
With custom post type templating, custom theme creators can more easily create subscription and membership sites by restricting the content on these specific posts and pages to specified user groups.
A Quick Overview of WP Taxonomies
I won’t go into too much detail here as Paul Kaiser has already written a great overview of taxonomies and the code that is involved to implement the function (see the tutorial here). The key word to remember when thinking about taxonomies is classification. They are similar to tags, but allow for deeper, more content-specific classification.
In truth, they are an extremely powerful way to group various items in all sorts of ways.
For example, say that I have use the Reviews post type from above. Because I will definitely want to classify that broad post type, I can create taxonomies like:
- Movie Reviews
- Book Reviews
- Product Reviews
With custom taxonomies, I could even go deeper into classifying the above taxonomies. I could go into movie genres, book authors, and product markets. Again, together with post types, these greatly increase Wordpress' content management capabilities.
Integrating Taxonomies via Functions.php
Overall, taxonomies are pretty easy to implement in your functions.php
file. Remember, you can go hierarchical with them or treat them like tags, so the more complex you want the greater the difficulty. Either way, here is a quick rundown of how to incorporate these into your theme.
Again, open up your functions.php file and insert the following code:
function movie_taxonomy() { register_taxonomy( 'movie_review', 'mysite_reviews', array( 'hierarchical' => true, 'label' => 'Movie Review', 'query_var' => true, 'rewrite' => array('slug' => 'movie-reviews') ) ); } add_action( 'init', 'movie_taxonomy' );
To break this down, first we give the taxonomy a formal name ("movie_review"), and we place it under the post type "mysite_reviews", which we created earlier.
function movie_taxonomy() { register_taxonomy( 'movie_review', 'mysite_reviews',
Then we pass these values:
array( 'hierarchical' => true, 'label' => 'Movie Review', 'query_var' => true, 'rewrite' => array('slug' => 'movie-reviews') )
This supplies the following arguments:
- hierarchical - When set to "true", the taxonomy will act more like a category. There can be parent taxonomies and nested taxonomies allowing for greater depth of classification. When set to "false", they act like just like tags.
- label - As with the post types above, this is the label that the taxonomy will publicly recieve.
- query_var - When set to "true" this taxonomy becomes a queryable element.
- rewrite - This sets the URL rewrite. Now posts in this taxonomy will be displayed as http://mysite.com/movie-reviews/{post title}/.
The end result within our admin nav should look like this:
Furthermore, we can dive into that interface and add more classification categories and structure. That interface looks similar to the category interface that you may already be familiar with.
From this interface, you can edit the slugs of the various categories within your taxonomy, create new categories, and determine parent and child categories.
For More Information
A full list of parameter arguments for taxonomies can be found here.
Displaying Taxonomies in Wordpress Themes
There are currently three primary ways to display custom post types in your themes:
- Taxonomy Cloud
- Custom Taxonomy Query
- Custom Taxonomy List
Taxonomy Cloud
Just as there are tag clouds, there are taxonomy clouds. To make it easy, both tags and clouds use the wp_tag_cloud
function. In order to display an array of taxonomy categories in a cloud, we would use the following code:
<?php wp_tag_cloud( array( 'taxonomy' => 'taxonomy_name_1','taxonomy_name_2' ) ); ?>
For more information about the function and its parameter arguments, visit the codex.
Custom Taxonomy Query
Taxonomies can also be included in custom queries just like we did with post types above. For example, to display content from the taxonomy movie_genre
, we would need to insert the following code into our template file:
$args = array( 'tax_query' => array( 'taxonomy' => 'movie_genre', 'field' => 'slug', 'terms' => 'comedy' ) ); query_posts( $args );
First, we use the argument tax_query
so that we can pass parameters that will allow us to query by slug or terms and return more accurate query results. In this example, we displayed posts tagged as "comedy" within the custom taxonomy "movie_genre". Once again, like we did with post types, we can limit the number of posts that this query returns.
Custom Taxonomy Lists
To display a comma-delineated list of posts by taxonomy, we simply need to put the following somewhere in the loop:
<?php the_terms( $post->ID, '{taxonomy name}', '{Displayed Title}: ', ', ', ' ' ); ?>
There are other display options for this list, and the parameters can be found here.
Using Post Types and Taxonomies in Your Next Project
Now that we have the nuts and bolts, I think "Why do I care about these things?" is a really fair question. The possibilities with post types and taxonomies, especially working with clients that are often too busy to manage the intricacies of a site, is endless. This, in my opinion, saves time over creating new admin functions and updating plugins, and opens doors to new possibilities with much less work (and coding) involved.
Below are some ideas that I had about how to integrate these into actual work:
1. Review Sites
First, a review site could benefit from the custom templating options listed above (single and archive)
Here are some more post types and taxonomies that you could implement on a review site:
- Post Type - Movie Reviews
- Taxonomies - Genre, Actor, Director
- Post Type - Book Reviews
- Taxonomies - Genre, Author, Publisher
- Post Type - Product Reviews
- Taxonomies - Product Category, Price
2. Real Estate Listing Sites
While there are some good templates for real estate listing sites, post types and taxonomies let creative people make custom templates that can easily be maintained by clients and webmasters alike. Here are just a few ideas to use in your next project:
- Post Type - Listings
- Taxonomies - Area, Agent, Price, Rooms
- Post Type - Agents
3. Event Listings
One thing that Wordpress really lacks (in my opinion) is a good event management system. Post types and taxonomies could easily take care of that by offering a system to classify monthly events by location, day, or any other system you can think of.
4. Subscription Sites
With the creation of a new post type and template, you could create a custom, premium category for your blog or website and integrate a payment gateway to create your very own membership site.
5. E-commerce
By creating a product post type and template, you could easily add and display products for sale on your site. In addition, taxonomies would allow for easy product organization.
Where Do We Go From Here?
I hope this tutorial has explained post types and taxonomies in Wordpress 3.0, and why they are an extremely valuable resource to implement in your projects. I know that there are many advocates of plugins (and I am a plugin author myself), but I hope that I have made the case that implementing these things is a very simple task and one that will save time for both you and your client in the long run.
The list of ideas above is a small one at best, so I would love to hear how you have implemented them into your projects or maybe some other uses that I didn't list.
Thank you so much for reading!
Comments