Show posts from other languages
-
My setup is an English and Greek bilingual installation. However since my visitors understand both languages, their browser defaults may prefer either. But they may never visit the second language, therefore certain untranslated posts won’t be visible to them.
I want to show posts from all languages in the archives, unless they have a proper translation of course. To keep content semantically and visually separated, I am using the lang attribute and CSS3 to style posts differently.
I have managed to work around it using code from an older post, but cannot get it to work with the default language.
I’m afraid my obstacle is inherent to how Polylang works. Is there any chance that we can see this functionality in the plugin Settings? Any way to modify my code to make it work?
add_filter( 'pre_get_posts', 'get_default_language_posts' ); function get_default_language_posts( $query ) { if ( $query->is_main_query() && is_home() && function_exists('pll_languages_list') && !is_admin()){ $languages = pll_languages_list( array( 'hide_empty' => 1 ) ); $terms = get_terms('post_translations'); $lang_default = pll_default_language(); $lang_current = pll_current_language(); $post_ids = array(); foreach($terms as $translation){ $transPost = unserialize($translation->description); if($transPost[$lang_current] != 0) { //has no translation in $lang_current $post_ids[]=$transPost[$lang_default]; } } if($lang_default!=$lang_current){ //without this check, default language returns zero posts $query->set('lang' , join( ',', $languages ) ); $query->set('post__not_in', $post_ids); } } return $query; }
- The topic ‘Show posts from other languages’ is closed to new replies.