Lessons Learned Custom Post Type Archives In Wordpress

03 November 2010

Since WordPress 2.9 there is a new feature I really like, and in the current project I am doing right now I found something I didn’t know.

To give a quick summary, since a few months I work for the Adobe User Group Netherlands. And together with my partner in crime Niels de Keizer I am building a mobile website for the Adobe User Group XL event. This website will be the guide for all visitors of the event, and contains a map, the program, and a description of all speakers. We decided to make this mobile website in WordPress because it fits to our needs and it’s an efficient way to build something like this.

We created some custom post types, which is a commonly used feature since WordPress 2.9. With this feature you can easily create custom types of content for your website. This is the menu we build in the WordPress admin for the AUGXL website:

The code you need to build the sponsors item is below:

register_post_type( 'sponsors',
	array(
		'labels' => array(
			'name' => __( 'Sponsors' ),
			'singular_name' => __( 'Sponsors' ),
			'add_new' => __( 'Add sponsor' ),
			'add_new_item' => __( 'Add new sponsor' ),
			'edit' => __( 'Edit' ),
			'edit_item' => __( 'Edit sponsor' ),
			'new_item' => __( 'New Sponsor' ),
			'view' => __( 'View sponsors' ),
			'view_item' => __( 'View sponsor' ),
			'search_items' => __( 'Search for sponsor' ),
			'not_found' => __( 'Sponsor not found' ),
			'not_found_in_trash' => __( 'No sponsors in trash' ),
			'parent' => __( 'Parent' ),
			),
		'public' => true,
		'show_ui' => true,
		'_builtin' => false,
		'capability_type' => 'post',
		'hierarchical' => false,
		'supports' => array('title', 'editor','thumbnail','revisions','custom-fields','post-thumbnails')
		)
);

When you create a sponsor it gets a custom link like www.website.com/sponsors/sponsor-xyz and WordPress loads this from the single-sponsor.php template page.

That’s awesome, but there comes the problem, you also need a list of all sponsors. By default this is all done in the index.php and if you have a lot of custom post types your index.php gets really messy!

The best thing to do would be to redirect www.website.com/sponsors to sponsors.php. This is not possible by default. However this will be in the next major version of WordPress! For now you need this plugin by Rolands Atvars. If you use this plugin you create a list of custom post type items (a custom post type archive) in separate php files. Awesome!

The plugin looks like this:

This is a preview of the mobile website we are building:

Jankees van Woezik profile picture

Hello, I'm Jankees van Woezik

Like this post? Follow me at @jankeesvw on Twitter