Using custom post types and custom taxonomies, we were able to turn icanlocalize.com from a big mess into an elegant website, that’s easy to maintain and translate.
It was really fun building icanlocalize.com using WordPress. The visual editor and media management make it easy to get contents online quickly. But, like many newly weds, after the initial excitement, real problems begin surfacing.
Custom Types Turn WordPress into a CMS
A content management system lets you edit contents without having to worry about how these contents appear on the page.
For example, have a look at this website translation page:
This page includes:
- Page title and body
We wanted testimonials to automatically appear in the right pages. For instance, this page only includes testimonials of clients who use our website translation service. Other testimonials that talk about iPhone localization, don’t appear in this page. They would appear in the iPhone localization service page.
Until WordPress 3, you could build this using pages and custom fields. That’s great, but causes a big mess. It’s difficult to set up and even worse to maintain. And, it gets much worse when running multilingual.
Now, it’s all clear and simple. Let’s start with the setup.
Adding Custom Types from the Theme
Since the testimonials type is added to our site, the place to declare them is in the theme. All it takes is this code:
register_taxonomy( 'service_type', array('post', 'page'), array( 'hierarchical' => true, 'label' => 'Service type', 'query_var' => true, 'rewrite' => true ) ); register_post_type( 'testimonial', array( 'description' => __( 'Testimonials.' ), 'labels' => array( 'name' => __( 'Testimonials' ), 'singular_name' => __( 'Testimonial' ), 'add_new' => __( 'Add New' ), 'add_new_item' => __( 'Add New Testimonial' ), 'edit' => __( 'Edit' ), 'edit_item' => __( 'Edit Testimonial' ), ... 'taxonomies' => array('service_type'), ...
Once we’ve added this to the theme, we get a new Testimonial section in the WordPress admin.
Displaying Custom Types in Standard Pages
So now, we were able to create testimonials as a new type. We’ve even added a service-type tag which says what the testimonials are about.
Next, we need to actually display the testimonials in pages.
We will collect all the testimonials that before to each service type and add them to that page.
What we do is:
$query = array('suppress_filters'=>0, 'post_type'=>'testimonial', 'service_type'=>$terms->slug, 'showposts'=>$number);
And, we’re done. Each page would automatically display all the testimonials for the service it talks about.
Multilingual, Out of the Box
WPML creates copies of the data in each language and associates them as translations. If you’re translating a testimonial, you’re actually creating another testimonial page in a different language. WPML assigns the language values and joins all translations together.
When our code requests the testimonial for a given service-type, it will automatically get them in the right language. This is because the service types are translated too and each translated page is automatically associated with the service type in the right language.
This may sound a bit complicated, but it all happens behind the scenes. In practice, you don’t need to do anything. Just translate everything and things ‘magically’ display in the right language.
Teaser – Using Views to Display Custom Data
In order to display testimonials on each page we had to write a piece of PHP code as a plugin. We love PHP and do it all day, but not everyone is like that.
Wouldn’t it be great if a plugin could display custom data in flexible ways? We’d like to be able to paginate, sort and do all sorts of basic operations.
We’ll supply the HTML / CSS and the plugin would get us the data to display.
Drupal folks are already giggling. It’s called Views – one of the most popular Drupal modules.
We’ve already started working on Views for WordPress. There’s no release yet, so this page doesn’t even exist, but it’s getting there. Once we’re done adding support to all the cool new features of WordPress 3 for WPML, we’re getting back to Views.