Custom Post Type tips: Wordpress 3.0

Custom-post-type-tips-wordpress-3.0

Custom Post Type tips: WordPress 3.0

WordPress 3.0 gives you the capability to add your own custom post types and to use them in different ways. For LightSpeed we’ve decided to add a new post type titled: “Portfolio”.

Implementing the new post type delivered the following problem:

The Portfolio page is a static page with a custom template and a loop querying the custom post type, WordPress called up the page, but the pagination did not work and kept on delivering the first page of the Portfolio.

Investigation

Playing with the permalinks we found that with the permalink settings on numeric it worked, but with the custom structure it failed. Wordpress tried to redirect to the page, but did not succeed. We came to the conclusion that Custom Posts and custom redirects where not set up properly.

The Fix

1. Register Custom Post types properly

Include the SD_Register_Post_Type class and the helper function sd_register_post_type(), then all you need in your functions.php file is the following line:

sd_register_post_type( ‘portfolio’ )  - Substitute Portfolio with the name of your custom post type.

2. Add Redirect to allow WordPress to use custom landing pages – like  a post listing page

Over at C. Murray Consulting we found a part of the solution.

This plug-in provides templates and friendly permalinks that enable “archives” for custom post types, like the “blog home” is an archive for all “post” post types. However,it only adds custom post type archives for non-hierarchical (post-like), public custom post types with a “slug” or permalink.

It also:

  • Adds support for custom post type archive permalinks, i.e. yourblog.com/custom-post-type-name – including paging (/page/2/)
  • Adds two new template files to the hierarchy, type-(custom-post-type-name).php and type.php
  • Adds new body classes to custom post type archives, custom-post-type-archive and custom-post-type-(post-type-name)-archive
  • Fixes the is_home conditional check on custom post type archives (incorrectly reports true by default)
  • Adds a new conditional, is_custom_post_type_archive for use in your themes
  • Fixes the wp_title output on custom post type archives to show the custom type’s label

Both Solutions where implemented and we’ve been smiling all the way.

One Comment

  1. Wow,Fantastic article,it’s so helpful to me,and your blog is very good,I’ve learned a lot from your blog here,Keep on going,my friend,I will keep an eye on it,One more thing,thanks for your post!.

Leave a Comment