How can I show a list of custom post type in different languages, using WPML plugin?

921 views Asked by At

In file functions.php I created a custom category

$labels = array(
    'name' => _x('Partners', 'post type general name'),
    'singular_name' => _x('Partners', 'post type singular name'),
    'add_new' => _x('Add New', 'partners'),
    'add_new_item' => __("Add New Partners"),
    'edit_item' => __("Edit Partners"),
    'new_item' => __("New Partners"),
    'view_item' => __("View Partners"),
    'search_items' => __("Search Partners"),
    'not_found' =>  __('No partners found'),
    'not_found_in_trash' => __('No partners found in Trash'), 
    'parent_item_colon' => ''
  );
  $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'menu_icon' => 'dashicons-groups',
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array('title','thumbnail','excerpt', 'editor')
  ); 
  register_post_type('partners',$args);

In the Partner's template I wrote:

<?php
        $args = array(
        'posts_per_page' => -1,  // Get all posts
        'post_type' => 'partners',  // Query for the default Post type
        'order_by' => 'post_date'  // Order by date posted
        );
        $last_five_posts = get_posts( $args );


        foreach ( $last_five_posts as $post ) : setup_postdata( $post );



?>

And it works fine. The page show me all posts I edited in the custom category.

The problem came out with translation. I use WMPL plugin, I use two languages: italian and english. The Partners'page show me both languages:

Italian page:

  • post ORIGINAL1

  • post TRANSLATED1

  • post ORIGINAL2

  • post TRANSLATED2

  • ...

English page:

  • post ORIGINAL1

  • post TRANSLATED1

  • post ORIGINAL2

  • post TRANSLATED2

  • ...

I think the problem is on the code, I'm missing something.

Do you know how to fix it?

1

There are 1 answers

0
B_etta On BEST ANSWER

The Support answered me. In the Partner's template instead of using the get_posts () function, I had to use the WP_Query class:

<?php
    $args = array(
    'posts_per_page' => -1,  // Get all posts
    'post_type' => 'partners',  // Query for the default Post type
    'order_by' => 'post_date'  // Order by date posted
    );
    $last_five_posts = new WP_Query( $args );


    if( $last_five_posts->have_posts() ): while( $last_five_posts->have_posts() ) : $last_five_posts->the_post();
  ?>