This post has three parts. This is part two. Visit the other parts below:
Really WordPress 3.0
Custom Post Types
Custom Post Types are my new best friends. I have used new post types on every client project since the beta of WordPress 3.0. That’s how valuable I think they are.
For any content that you need to act like a post, but not actually be a post, custom post types are there for you. If you already have a news section that uses posts, but also want an articles section separate from your posts, custom post types are what you need.
One of my latest site designs, stuartclayton.com, I used no fewer than four different custom post types:
- #### Discography
It’s pretty self-explanatory, I used the custom post types to list a new CD in each post.
- #### Books
Each one of Stuart’s books has its own post in the ‘Books’ post type.
- #### Features
This is a bit less obvious. In the sidebar or footer on every page is a feature that shows a latest book, video or audio track.
For this I used a custom post type of ‘Feature’ with a featured image to show the image and using the title and text content (even though it’s very little text) as standard. The ‘Buy Now’ button is created by entering a URL and button text into a custom write panel (also known as meta boxes.)
- #### Gear
The gear is pretty special as I made use of a ‘Gear’ custom post type with a ‘Type of gear’ custom taxonomy. You certainly couldn’t do that a few versions ago!
Each item of musical gear that Stuart has is (or will be) listed under the ‘Gear’ section. Then to group the different gear by whether it’s a bass, an amp, strings or effects, a category-style custom taxonomy is used. This helps dictate how the pages are laid out.
I struggled a bit with custom post types at first. I think it It was probably because the developers hadn’t quite ironed out all the quirks and I was too keen on trying out everything during the beta. I could create custom post types that showed up in the admin with no problem, but had issues with choosing which features showed in the editors.
If you, like me, get a bit confused with the huge amount of options for custom post types in the functions.php file, I really would recommend you give Custom Post Type UI by WebDevStudios a go. This excellent plugin does just what you would do in your functions.php file but gives it a friendly checkbox and text input interface in your WordPress admin so you needn’t write any code.
For those that more confident, these are the best resources I’ve found on creating custom post types straight into your themes (or plugins):
- Custom Post Types in WordPress by Justin Tadlock (I know, I’m a fan!)
- Custom Post Types on the WordPress Codex
- How to add a custom icon to your custom post type by Ptah Dunbar
loop.php and other Template Parts
A change in WordPress 3.0 that I’m a bit confused about is the template parts, which separates the WordPress loop into its own file, loop.php. It is certainly not necessary to do this in order to make your WordPress theme work, and the idea behind doing this is that you can now easily separate repeatable parts of your templates into different files and call them when you need them, thus preventing you repeating yourself many times in one file and needing to edit the same repeated line of code multiple times just because you want to change a few characters.
However, the implementation of this in the new default TwentyTen theme seems to contradict this non-repetitive, breaking up into digestible chunks, sensibility. Apologies for the huge amount of code that follows:
can be overridden in child themes with loop.php or * loop-template.php, where ‘template’ is the loop context * requested by a template. For example, loop-index.php would * be used if it exists and we ask for the loop with: *
get_template_part( ‘loop’, ‘index’ );* * @package WordPress * @subpackage Twenty_Ten * @since Twenty Ten 1.0 */ ?>
max_num_pages > 1 ) : ?>
>$post->ID, ‘post_type’ => ‘attachment’, ‘post_mime_type’ => ‘image’, ‘orderby’ => ‘menu_order’, ‘order’ => ‘ASC’, ‘numberposts’ => 999 ) ); $total_images = count( $images ); $image = array_shift( $images ); $image_img_tag = wp_get_attachment_image( $image->ID, ‘thumbnail’ ); ?>
| ‘ ); ?>
> →’, ‘twentyten’ ) ); ?>
| ‘ ); ?>
→’, ‘twentyten’ ) ); ?> ‘‘ ) ); ?>
%2$s’, ‘twentyten’ ), ‘entry-utility-prep entry-utility-prep-cat-links’, get_the_category_list( ‘, ‘ ) ); ?> %2$s’, ‘twentyten’ ), ‘entry-utility-prep entry-utility-prep-tag-links’, $tags_list ); ?> | ‘ ); ?>
``` If you wade through the code above, you might notice that the loop is cycling through special displays for a gallery category, an asides category, the archives and search, and then all other posts. This seems utterly bizarre to me. Why, in an exercise that is aimed at splitting the code into manageable chunks are they lumping all of these different outputs into one new file? According to the [Template Hierarchy](http://codex.wordpress.org/Template_Hierarchy#Category_display), you could have separate template files for * a category named ‘gallery’ (category-gallery.php) * a category named ‘asides’ (category-asides.php) * an archive page (archive.php) * and search results (search.php.) Leaving all other posts happily served by index.php. This would keep your templates separate and easy to read as well as stopping WordPress having to cycle through all of those conditional options by just hitting the relevant template first. As I’ve said before, I’m no PHP developer, but if somebody could explain the logic of this particular loop.php file to me, it’d make me feel a lot happier! --- This post has three parts. This is part two. Visit the other parts below: * Part 1: [WordPress 3.0—Pre 3.0 and Beyond](/wordpress-3-0-pre-3-and-beyond) * Part 3: [WordPress 3.0—Theme Development and the Design](/wordpress-3-0-theme-development-and-the-design) ## 6 comments