How to display on the blog page the posts corresponding to the selected language, using Polylang

I use Polylang (free) to internationalize a customer site. I set a default language (= FR), I added a language switcher in my navbar (FR and US flags) and I set the Front and Blog pages for both languages.

1) The language switcher works well with the pages. If you go to the homepage (http://cecile-chancerel-bijoux.com/wp/), you have the default FR homepage. When you click the US flag, it redirects you to the US homepage.

2) It’s also true for the blog page (displaying posts list). I have 2 different URLs for the FR and US blogs. You can switch between them with the langage switcher flags :

FR : http://cecile-chancerel-bijoux.com/wp/blog/

US : http://cecile-chancerel-bijoux.com/wp/blog-en/

The problem is they both display the FR AND the US posts. Obviously I already set the language for these 2 posts. Check the screenshot here : http://res.cloudinary.com/dbhsa0hgf/image/upload/v1522144114/blog_issue_with_polylang_x7ev8p.png

I want the US blog (/blog-en) to display only the posts in English and the FR blog (/blog) to display only the posts in French. How can I achieve this ?

EDIT :

I use Themify’s Elegant theme, thus I modified this file : wp-admin/wp_content/themes/themify-elegant/index.php like this :

I updated following block :

<?php// Loop?>
<?php if (have_posts()) : ?>
  <div id="loops-wrapper">
    <?php while (have_posts()) : the_post(); ?>
      <?php if(is_search()): ?>
       // some logic //
      <?php endif; ?>
    <?php endwhile; ?>
  </div>
<?php else : ?>
  <p><?php _e( 'Sorry, nothing found.', 'themify' ); ?></p>
<?php endif; ?>

with a custom query

<?php// Loop?>
  <?php $args = array(
    'post_type'      => 'post',
    'lang'           => pll_current_language('slug'),
    'posts_per_page' => 10,
    'post_status'    => 'publish',
  );?>
  <?php $query = new WP_Query( $args );?>

  <?php if ($query->have_posts()) : ?>
    <div id="loops-wrapper">
      <?php while ($query->have_posts()) : $query->the_post(); ?>
        <?php if(is_search()): ?>
         // some logic //
        <?php endif; ?>
      <?php endwhile; ?>
    </div>
  <?php else : ?>
    <p><?php _e( 'Sorry, nothing found.', 'themify' ); ?></p>
  <?php endif; ?>

And it works ! I eventually have only the posts in french displayed on my french blog page, same for the english one. Thanks @Friss, this is all thanks to you !

Guillaume

1 Answer
1

you can add the lang parameter to your loop arguments like so

$loop = new WP_Query( array (
        'post_type'      => 'post',
        'lang'           => pll_current_language('slug'), //returns 'en' for example    
        'posts_per_page' => 10,
        'post_status'    => 'publish',
) );

However this is not the better practice because by doing that way we override the main query which is not wp friendly.
We should better do like this using the pre_get_post action hook, so in your functions.php file:

if(function_exists('pll_current_language')) // if polylang
{
    add_action( 'pre_get_posts', 'include_language' );
    function include_language( $query ) 
    {
        if ( $query->is_main_query() ) { //add more condition here if needed
            $query->set( 'lang', pll_current_language('slug') );
        }
    }   
}

Leave a Comment