Skip Navigation

[Resolved] Language dependent content

This thread is resolved. Here is a description of the problem and solution.

Problem:

How to show content of a custom post type in a specific language while WPML is active

Solution:

In the WP_Query try to use the lang attribute:

<?php

$loop = new WP_Query( array (
        'post_type'      => 'YOUR_CUSTOM_TYPE_SLUG',
        'lang'           => 'en',    // use language slug in the query
     // 'lang'           => 'de,fr', // query German and French posts
     // 'lang'           => '',      // query posts in all languages
        'paged'          => get_query_var( 'paged' ),
        'posts_per_page' => 10,
        'post_status'    => 'publish',
) );

while( $loop->have_posts() ) : $loop->the_post(); ?>

    // html here

<?php endwhile; ?>
This support ticket is created 2 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 4 replies, has 2 voices.

Last updated by Paulo Ramalho 2 years, 5 months ago.

Assisted by: Christopher Amirian.

Author
Posts
#2497581

Hi.
I need to display latest news on a sidebar (single.php) based on the selected language. It could be a simple thing but I can't find out how to do. Some conditional maybe. I have a "News" CPT and it's translated by WPML.

I have this piece of code inside my sidebar.

    <?php
    global $post;
    $args = array(
    'post_type' => 'noticias',
    'posts_per_page' => 5 );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) : setup_postdata($post); ?>

  • ">
  • <?php endforeach; wp_reset_postdata(); ?>

I really apreciate any kind of help. Thanks.

#2497989

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

I am not sure what is the issue that you are experiencing. I wonder if the content does show but is not translated or if it is not showing at all?

Please consider that Toolset uses the normal WordPress method to add the CPT. And if you can not show the CPT in the original language then it is something wrong with your code.

I suggest that instead of get_posts you use the WP_Query method:

https://developer.wordpress.org/reference/classes/wp_query/

Thanks.

#2498155

Yes, I get a solution here.
hidden link
Thank you anyway.

#2498167

Christopher Amirian
Supporter

Languages: English (English )

Great. I'm glad that you managed to fix the issue.

#2498169

My issue is resolved now. Thank you!