1

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

  • My pleasure, glad to help a compatriot ;) – Friss Mar 29 '18 at 7:10
2

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') );
        }
    }   
}
  • YEEAAAH, it works with polylang function. My Edit is updated accordingly. I am so grateful for your patience and the time you spent to solve this question ! I suggest that we clean a bit the discussion. I will remove all my comments above. Maybe you can also change your answer so that it shows the actual solution. Thanks again, you ve made my day ! – guillaume Mar 29 '18 at 6:43
  • i did not understand, do i need both functions or just one? would you mind explaining? thank youuu – Alfrex92 Aug 22 '18 at 9:47
  • No, just one, depending on your needs. – Friss Aug 22 '18 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.