You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Polylang插件问题:多语言站点无翻译时默认语言文章不显示

Hey there! I’ve dealt with this exact scenario using Polylang on WordPress, so I know how annoying it is when content vanishes when switching to a non-default language. Let’s get this sorted so your visitors see default-language posts when no translation exists.

Option 1: Adjust Your Custom WP_Query (Direct Fix)

Polylang automatically filters queries to only show posts in the current active language. To include both current-language translations AND default-language posts where no translation exists, modify your WP_Query arguments to target both languages, then ensure you don’t display duplicate posts:

// Get current and default language codes using Polylang's built-in functions
$current_lang = pll_current_language();
$default_lang = pll_default_language();

// Query posts from both languages, with distinct results to avoid duplicates
$latest_blog_posts = new WP_Query( array(
    'posts_per_page' => 15,
    'lang' => array( $current_lang, $default_lang ),
    'post_status' => 'publish',
    'posts_distinct' => true
) );

// Loop through posts, prioritizing translated versions when available
if ( $latest_blog_posts->have_posts() ) :
    while ( $latest_blog_posts->have_posts() ) : $latest_blog_posts->the_post();
        // Check if a published translation exists for the current language
        $translated_post_id = pll_get_post( get_the_ID(), $current_lang );
        
        // Swap to the translated post if it exists
        if ( $translated_post_id && get_post_status( $translated_post_id ) === 'publish' ) {
            setup_postdata( get_post( $translated_post_id ) );
        }
        
        // Output your post content here (example)
        echo '<article>';
        echo '<h2>' . get_the_title() . '</h2>';
        echo '<div class="post-excerpt">' . get_the_excerpt() . '</div>';
        echo '</article>';
        
        // Reset postdata to prevent conflicts with subsequent queries
        wp_reset_postdata();
    endwhile;
endif;

Option 2: Global Filter (Apply to All Site Queries)

If you want this fallback behavior to work across your entire site (not just this one custom query), add these functions to your theme’s functions.php file. This adjusts all front-end main queries to fall back to default-language content when no translation exists:

// Modify main queries to include current and default languages
add_action( 'pre_get_posts', 'pll_fallback_to_default_language' );
function pll_fallback_to_default_language( $query ) {
    // Skip admin area and non-main queries to avoid breaking backend functionality
    if ( is_admin() || ! $query->is_main_query() ) {
        return;
    }
    
    $current_lang = pll_current_language();
    $default_lang = pll_default_language();
    
    // Target both languages in the query
    $query->set( 'lang', array( $current_lang, $default_lang ) );
    // Add distinct clause to remove duplicate posts (original + translation)
    add_filter( 'posts_distinct', 'pll_distinct_posts' );
}

// Helper function to add distinct SQL clause
function pll_distinct_posts( $distinct ) {
    global $wpdb;
    return "DISTINCT {$wpdb->posts}.ID";
}

Quick Notes:

  • Ensure Polylang is properly installed, activated, and your default language is set correctly in Polylang’s settings.
  • The pll_get_post() function is a Polylang native tool that retrieves the translated post ID for a given language—perfect for swapping to the translated version when it exists.
  • The posts_distinct parameter ensures you don’t end up showing both the original default-language post and its translation in the same list.

内容的提问来源于stack exchange,提问作者Sergi Khizanishvili

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:41:02