Default language posts in other languages
-
Dear Chouby
this is a topic which has been addressed several times in older versions of Polylang. However, given the new version of Polylang I wander if there has been an update since that allows for an easier approach to this problem.
Question: how do we display the default language posts for other languages which do not have a translation?
Pre-v1.0 of PolyLang, I found this topic that highlights the issue and provides a solution at the post loop in each template.
Furthermore, the solution provided in the polylang documentation is very similar.
However, in earlier versions of PolyLang there is also a discussion which point a 2nd plugin called “polylang displays all posts v0.2”.
So it would seem that at this point the only way to do this is to hack the post loop of each template file. This would actually require quite a lot of changes and result in replicating most of the parent theme files in the child theme. I am not very keen on this solution.
I have managed to solve this using the ‘pre_get_post’ hook which is ‘cleaner’ hack in my mind. Here is the code.
add_filter( 'pre_get_posts', 'get_default_language_posts' ); function get_default_language_posts( $query ) { if ( $query->is_main_query() && function_exists('pll_default_language')){ $terms = get_terms('post_translations'); //polylang stores translated post IDs in a serialized array in the description field of this custom taxonomy $defLang = pll_default_language(); //default lanuage of the blog $curLang = pll_current_language(); //current selected language requested on the broswer $filterPostIDs = array(); foreach($terms as $translation){ $transPost = unserialize($translation->description); //if the current language is not the default, lets pick up the default language post if($defLang!=$curLang) $filterPostIDs[]=$transPost[$defLang]; } if($defLang!=$curLang){ $query->set('lang' , $defLang.','.$curLang); //select both default and current language post $query->set('post__not_in',$filterPostIDs); // remove the duplicate post in the default language } } return $query; }
Would there be a way to define post content as default language regardless of language and enable this functionality within the plugin itself? Maybe for a future version?
kind regards
- The topic ‘Default language posts in other languages’ is closed to new replies.