Navigation überspringen

[Gelöst] Language dependent content

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

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 vor 3 years, 7 months. 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.

Dieses Thema enthält 4 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von Paulo Ramalho vor 3 years, 7 months.

Assistiert von: Christopher Amirian.

Author
Artikel
#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); ?>

  • versteckter Link
  • <?php endforeach; wp_reset_postdata(); ?>

I really apreciate any kind of help. Thanks.

#2497989

Christopher Amirian
Unterstützer

Sprachen: Englisch (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.
versteckter Link
Thank you anyway.

#2498167

Christopher Amirian
Unterstützer

Sprachen: Englisch (English )

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

#2498169

My issue is resolved now. Thank you!