9

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:

Page with different elements

This page includes:

  • Navigation
  • Page title and body
  • Testimonials

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.

Admin menu with testimonials

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[0]->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.

9 Responses to “Custom Types in WordPress 3 Simplified Our Site”

  1. At the current state (WPML 1.7.7) does the plugin support custom posts? In my installation doesn’t seem to be able to do this.

  2. I’ve created a custom post type like in the example above, but there are no translation controls displayed. How can I enable the translation features?

  3. Hi, i have a little question to do. I’m using WPML 2.0.5, and just recently, taxonomies are not translated anymore.

    I’m mean, I’m use a Woothemes theme, (Estate) and I have taxonomies for “Locations” and “Property Types”.

    Just a few days ago, when I’m made a post with a property, and I selected the taxonomy for Locations, and that property was translated, the property save ok in the database, with the location translated.

    Now, without touching any file on the site (i finished the project a few weeks ago) when i translate a property, don’t save in the database the location, wich makes the property translation accesible through the post himself, but don’t for search taxonomy. (the menu has the taxonomy “Locations” and the different locations) so, when you saw a spanish property and select english, the property shows ok, but when i search trhough menu all the property in a X location, the property doesn’t show on english!

    The site is http://www.confiahome.com and the first featured property (http://www.confiahome.com/property/chalet-en-venta-obra-nueva-2/) is in “Paracuellos de Jarama” if you see this location on spanish, the property will show off, but if you see the location on english, doesn’t show.

    Any clue? I’m asked for the theme guys, and they told me the some people was had this issue and you could help them.

    Thanks in advance! is a great plugin, and i want to keep it. =)