WooCommerce多语言产品仅显示关联语言旗帜的技术问询
Solution: Show Only Language Flags for Translated Products in WooCommerce
Got it, let's fix this issue where untranslated language flags lead to the homepage on product detail pages. The key is to filter Polylang's language list to only include languages that have a translation for the current product. Here are two straightforward methods:
Method 1: Modify Template Code Directly
Replace your existing pll_the_languages() call with this code in your product template file (like single-product.php or your custom theme's product template part):
<?php // Check if we're on a single product page if ( is_product() ) { global $product; $current_product_id = $product->get_id(); $available_languages = []; // Get all enabled languages from Polylang $all_enabled_langs = pll_languages_list(); // Loop through languages to check for product translations foreach ( $all_enabled_langs as $lang_code ) { $translated_product_id = pll_get_post( $current_product_id, $lang_code ); // Add language to list if a translation exists if ( $translated_product_id ) { $available_languages[] = $lang_code; } } // Output only flags for languages with product translations if ( ! empty( $available_languages ) ) { pll_the_languages( [ 'show_flags' => 1, 'show_names' => 0, 'include' => $available_languages, ] ); } } else { // For non-product pages, keep showing all enabled languages pll_the_languages( [ 'show_flags' => 1, 'show_names' => 0 ] ); } ?>
How this works:
- It first checks if we're on a single product page to avoid breaking other pages.
- Uses
pll_get_post()to verify if the current product has a translation for each enabled language. - Passes the filtered list of languages to
pll_the_languages()via theincludeparameter, so only valid translation flags are displayed.
Method 2: Use a Polylang Hook (Cleaner for Global Changes)
If you prefer not to modify template files, add this code to your theme's functions.php file. It uses a Polylang hook to automatically filter the language list on product pages:
add_filter( 'pll_the_languages_args', 'filter_product_translation_languages', 10, 1 ); function filter_product_translation_languages( $args ) { // Skip filtering if not on a single product page if ( ! is_product() ) { return $args; } global $product; $current_product_id = $product->get_id(); $available_languages = []; $all_enabled_langs = pll_languages_list(); foreach ( $all_enabled_langs as $lang_code ) { if ( pll_get_post( $current_product_id, $lang_code ) ) { $available_languages[] = $lang_code; } } // Update args to include only valid languages if ( ! empty( $available_languages ) ) { $args['include'] = $available_languages; } else { // If no translations exist, only show the current language $args['include'] = [ pll_current_language() ]; } return $args; }
How this works:
- The
pll_the_languages_argshook modifies the parameters passed topll_the_languages()globally. - It automatically filters out languages without a product translation on single product pages, while leaving other pages unaffected.
- Your original template code (
<?php pll_the_languages(array('show_flags'=>1,'show_names'=>0)); ?>) stays exactly the same.
Important Notes:
- Ensure the WooCommerce Polylang Integration plugin is active, as it handles the link between WooCommerce products and Polylang translations.
- Test this with products that have partial translations to confirm only valid flags appear.
内容的提问来源于stack exchange,提问作者user11427967
相关产品推荐
相关产品推荐

