Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Support Chrystl

    (@chrystl)

    Thread Starter fokus3

    (@fokus3)

    Hi Chrystl,

    yes I did. The first code examples on this thread do not work and the code below in the post with the “continue” statement in the loop was working (after some modification), but is breaks the loop.

    Breaking the loop means, that I will show 5 posts on a page and when the “continue” condition is sometimes true then less then 5 posts are shown (obviously).

    In other words: the solution itself is working but is not really usefull and the problem still exist.

    Thread Starter fokus3

    (@fokus3)

    Hi there,

    is it possible that this suggested piece of code does not working anymore in WP 4.2.4? Is there another solution for mixed multi-lang content available?


    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;
    }

    Plugin Support Chrystl

    (@chrystl)

    Thread Starter fokus3

    (@fokus3)

    no way, unfortunaly this function doesn’t work. nothing happens ?? do you have any other idea?

    Thread Starter fokus3

    (@fokus3)

    this seems to be the only solution that works a bit. Bt this will break pagination (404 on page > 1).

    Is there really no working solution without changing the loop? In WPML this works like a charme… and I guess it should be also possible in PL in a smart way…?

    global $wp_query;
    
                $categories = get_the_category();
                $category_id = $categories[0]->cat_ID;
                $deCatId = pll_get_term($category_id, 'de');
    
    			$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    			query_posts(array_merge($wp_query->query, array(
    				'post_type' => 'post',
    				'paged' => $paged,
    				'lang' => 'de',
    				'cat'=> $deCatId,
    				'category_name' => null,
    				'suppress_filters' => true
    			)));
    		?>
    
    		<?php if (have_posts()) : ?>
    			<?php while (have_posts()) : the_post() ?>
    
    				<?
    					if (function_exists('pll_current_language'))
    						if($post_id = pll_get_post(get_the_ID(), pll_current_language())) {
    							$post = get_post($post_id);
    							setup_postdata($post);
    						}
    				?>
    ...

    It’s normal the function I adapt from Syllogic don’t work. You have to adapt it for you. Do you use french and english language ?

    Thread Starter fokus3

    (@fokus3)

    I have adapted the function to english. So, German is the fallback language.

    Did you try what ? :

    add_filter('pre_get_posts', 'get_default_language_posts');
    
    function get_default_language_posts($query)
    {
        if ($query->is_main_query() && isset($query->query['lang']) && !is_admin()) {
            $terms = get_terms('post_translations'); //polylang stores translated post IDs in a serialized array in the description field of this custom taxonomy
            $curLang = $query->query['lang']; //current selected language requested on the broswer
            $otherLang = $curLang === "de" ? "en" : "de";
            $filterPostIDs = array();
            foreach ($terms as $translation) {
                $transPost = unserialize($translation->description);
                $filterPostIDs[] = $transPost[$otherLang];
            }
            $query->set('lang',  $curLang. ',' . $otherLang);  //select both default and current language post
            $query->set('post__not_in', $filterPostIDs); // remove the duplicate post in the default language
        }
        return $query;
    }

    Thread Starter fokus3

    (@fokus3)

    Yes, I did, but when using this it shows me not the translation if available. I also played arround with the query itself, but I get all posts (original or tranlsted), or only the german or only the english.

    I played with this code and your provided function
    `query_posts(array_merge($wp_query->query, array(
    ‘post_type’ => ‘post’,
    ‘paged’ => $paged,
    ‘cat’ => ‘-22,-544’,
    ‘lang’ => ‘de’,
    ‘suppress_filters’ => true
    )));`

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Show default language if no translation is available’ is closed to new replies.