Search

Diseño Web HTML5 CSS3 Javascript

Diseño Web – Amit Kvint

Como Crear y Traducir Entradas Personalizadas

Create and translate custom post types

Introduction

In this article, we will explain what custom post types are. In addition, we will show you how to create custom post types and how they can help in organizing and adding your website content. We will also add custom fields using the Types plugin. Moreover, we will demonstrate translating custom post types using WPML.

What are “Custom Post Types”?

Pages, posts, attachments, revisions, and navigation menus are the default WordPress post types; but what does that even mean? To be more clear and specific we can refer to “Custom Post Types,” also known as CPTor “Custom Content Type.” For example, Pages are pieces of content of the post type “page”; similarly, Posts are pieces of content of the post type “post.”

Let us say that you have a website that reviews games. You will probably want to add reviews for more than one game. All games share some information (metadata) like release date, genre, platform, system requirements, screenshots, etc. In this case, you will want to create a custom post of type “game” and add custom fields to include the metadata. Other examples of custom post types could be Portfolios, Testimonials, and Products.

Once you have created the custom post type game, it will show up on your WordPress dashboard as a separate menu item. You can also use the default categories available or create custom taxonomies suitable for the content. In addition, you will have the opportunity to choose how the custom post type will be displayed on the front-end. Here, you can read more about Post Types.

Why should I use “Custom Post Types”?

The general idea behind using custom post types is to make adding and displaying some specific pieces of content with the same structure or format easier. There are many indications of when you should start using a custom post type:

  • When you do not need these pieces of content to have a chronological hierarchy;
  • Additional fields are needed to add more details about the post;
  • You want to display your posts in a specific way on the front-end;
  • More categories and tags are needed to classify the pieces of content.

Creating custom post types and custom taxonomies will make it easier for your users to browse and sort your custom posts. For example, games can be sorted and searched by ( genre, release date, etc).

The CPTs allows you to have pretty permalinks as they will show the custom post type name and the post name. For example, the URL for a Game post will be https://mysite.com/game/game-name

How to create “Custom Post Types”?

It is easier to create Custom Post Types using a plugin; however, you can also add some code to your functions.php theme file to add a Custom Post Type and custom fields to the theme you are using.

You can add the code below to the functions.php file of your theme to create a custom post type; in this example, we will create the post type game. Note that you can add this code to the end of the functions.php file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
add_action( 'init', 'create_post_type');
function create_post_type() {
    $labels = array(
    'name'               => _x( 'Games', 'general name of the post type' ),
    'singular_name'      => _x( 'Game', 'name for one object of this post type' ),
    'add_new'            => _x( 'Add New', 'game' ),
    'add_new_item'       => __( 'Add New Game' ),
    'edit_item'          => __( 'Edit Game' ),
    'new_item'           => __( 'New Game' ),
    'all_items'          => __( 'All Games' ),
    'view_item'          => __( 'View Game' ),
    'search_items'       => __( 'Search Game' ),
   );
   $args = array(
     'labels' =>  $labels, // An array that defines the different labels assigned to the custom post type
     'public' =>  true, // To show the custom post type on the WordPress dashboard
     'supports' =>  array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ), // Adds support for comments, revesions, etc
     'has_archive' =>  true, //Enables the custom post type archive at http://mysite.com/post-type/
     'hierarchical' =>  true, //Enables the custom post type to have a hierarchy
     'rewrite' => array( 'slug' =>  _x('game', 'URL slug')), //To be able to translate the custom post slug using WPML
);
register_post_type( 'game', $args );
}

Indeed, this is the “geeky” way to create custom post types. An easier method is to use a plugin such asTypes. It is a free plugin that will allow you to create your own CPTs, custom fields, and custom taxonomies, effortlessly. In addition, Types documentation provides clear and detailed instructions to create your custom post type in a few minutes. We will use it to add the custom fields to our custom post type Game. You can also check the full documentation of adding custom fields.

After you install and activate Types, a new menu item will show on your WordPress dashboard. Follow the instructions below to add our custom fields to the CPT Game:

  1. Navigate to Toolset -> Add New Post Field Group.
  2. Click on the Add New Group button.
  3. Fill the Name field and click on the Save Field Group button.

Crerate custom field group

    1. Assign the post field group to the CPT Game by clicking on the Edit button found on the “Where to include this Field Group” section and choose Games as illustrated below:

Assgin field group to custom post type game

    1. Click on the Add New Field button and choose the field type. For example, we will create the custom fieldsrelease date and genre. We will choose the field type Date for the release date. Finally, we will set the field options and click on the Save Field Group button.

Release date custom field

  1. To create the genre custom field, click on the Add New Field button and choose the field type to be Single Line. Set the field options and click on the Save Field Group button.

Genre custom field

This is how our CPT Game will look on the WordPress dashboard:

CPT on the front-end

You can use the Views plugin to display the Games CPT on your front-end. Views documentation will provide you with the information you need to create content templates to display your custom content type they way you want. Here is an example of how the Games CPT will be displayed on the front-end using the Views plugin:

Custom post type front-end example

How to translate “Custom Post Types”?

With WPML you can translate the CPTs that come with your theme, CPTs that you have created using a plugin, and CPTs that you have coded as shown in this tutorial. To set the CPT (Game) that we have created for translation, you will need to install and activate WPML’s core plugin (sitepress multilingual cms) along with the Translation Management add-on.

    1. Navigate to WPML -> Translation Management and click on the Multilingual Content Setup tab and scroll down to the Custom Posts section.
    2. Set the CPT Game to be translated.

Set CPT to be translatable

    1. Click on the Save button.
    2. Go to the Custom Field Translation section and set the custom field genre to translate and the release date to copy from original to translation since we don’t need to translate the date.
    3. Click on the Save button.

Set custom fields to be translatable

    1. Navigate to Games -> All Games. Click on the + icon to add your translation.

Translate Game

  1. Click on the Copy content from English button.
  2. Add your translated text and click on the Publish button.

Add your translated texts

Here is an example of how the translated CPT (Games) would look like on the front-end:
Translated custom post type front end

Conclusion

Using CPTs makes it easier to manage your content. If you are running a multilingual website, you will want to translate your CPTs, custom field type, and custom taxonomies. WPML can translate all these elements efficiently.

Matt Mullenweg en WCEU

Sobre la libertad de expresion, politica, codigo abierto y el futuro de WordPress

WordPress Cadiz MeetUp – Creando temas más rápidos con Grunt.js

Esta semana nos veremos en Jerez – http://www.meetup.com/es-ES/Cadiz-WordPress-Meetup/events/230121584/

Blog at WordPress.com.

Up ↑

Follow

Get every new post delivered to your Inbox.

Join 1,530 other followers

%d bloggers like this: