WordPress.org

Ready to get started?Download WordPress

Forums

Polylang
[resolved] Showing default language post if no translation is available (13 posts)

  1. Rutja76
    Member
    Posted 2 years ago #

    Hi all

    I have a website which is mainly in English, where only some pages and posts are translated in other languages.

    In Polylang, when choosing a particular language, only posts translated in that language are shown.

    Would it be possible to show the post in the default language if there is no translation available?

    Same thing should also be done also for e.g. category names which are not translated.

    Apologies if this issue has already been discussed, couldn't find it in the archive.

    Cheers,

    http://wordpress.org/extend/plugins/polylang/

  2. Chouby
    Member
    Plugin Author

    Posted 2 years ago #

    Here is a proposed solution. You will need the current development version (or future 0.8.3), and modify the loop in your theme for example like this.

    <?php query_posts(array('post_type' => 'post','lang' => 'en')); // force querying the English posts ?>
    	if (have_posts()) : ?>
    		<?php while (have_posts()) : the_post(); ?>
    		<?php global $post;
    		if($post_id = pll_get_post($post->ID, pll_current_language())) { // get translated post (in current language) if exists
    			$post = get_post($post_id);
    			setup_postdata($post);
    		}?>
    ... do what ever you want in your loop ...
  3. renemorin
    Member
    Posted 1 year ago #

    Hi there,

    I have the same need. My website is French/English, with French as the default language. I would like French posts to be displayed also on the French home page when no English translation is available. I tried copying the above code in index.php (at the end), but then I'm getting a parse error like this:

    Parse error: syntax error, unexpected $end in /hermes/bosweb/web069/b695/nf.laboiteamots1/public_html/laboiteamots.ca/wp-content/themes/twentyeleven/index.php on line 61

    I'd be most grateful if you could help me fix this. This is all relatively new to me so please be very specific as to what I need to do.

    Nice work on Polylang, by the way. I love it!

    Cheers,

    Rene

  4. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    For Twenty Eleven, there are several files to modify :
    archive.php, author.php, category.php, index.php, tag.php

    I will give an example for index.php supposing the default language is French (the modification to do in other files is similar). Replace these lines:

    <?php if ( have_posts() ) : ?>
    
    	<?php twentyeleven_content_nav( 'nav-above' ); ?>
    
    	<?php /* Start the Loop */ ?>
    	<?php while ( have_posts() ) : the_post(); ?>
    
    		<?php get_template_part( 'content', get_post_format() ); ?>
    
    	<?php endwhile; ?>

    by:

    <?php // force querying the French posts
    global $wp_query;
    query_posts(array_merge($wp_query->query, array('lang' => 'fr'))); ?>
    <?php if ( have_posts() ) : ?>
    
    	<?php twentyeleven_content_nav( 'nav-above' ); ?>
    
    	<?php /* Start the Loop */ ?>
    	<?php while ( have_posts() ) : the_post(); ?>
    		<?php // get translated post (in current language) if exists
    		global $post;
    		if($post_id = pll_get_post($post->ID, pll_current_language())) {
    			$post = get_post($post_id);
    			setup_postdata($post);
    		}?>
    
    		<?php get_template_part( 'content', get_post_format() ); ?>
    
    	<?php endwhile; ?>

    NB1: The query_posts line is not OK on page 2, 3 ... in my previous proposed solution (and in the FAQ included with Polylang 0.8.6). I will correct it in the next release.
    NB2: You must have *all* posts in the default language for this to work correctly

  5. renemorin
    Member
    Posted 1 year ago #

    Hi Chouby,

    Mucho thanks for your input! I tried the above on my index.php file, but what it does is replacing the post that was on the home page with the contents of a 2nd-level page (not a post!).

    What I want to do has to do with only posts (not pages). I understand Rutja76 wants to do the same. For instance, if my latest post is in French and there's no translation for it, I'd like it to be shown on both French and English home pages. Likewise, if my latest post is in English and there's no translation for it, I'd like it to be shown on both French and English home pages. But, if I'm providing a translation for a post, then of course the English version would be posted on the English home page and the French version would be posted on the French home page. Follow me?

    If you can help with that, that would be fantastic! In any case, thanks for your consideration have a great weekend!

    Rene

  6. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    I do not understand what you mean by 2nd level page. What the code provided above should do:
    * On the French home page: nothing special
    * On the English home page: show the French post if it has not been translated in English.

    However it works only in this direction as stated previously in 'NB2'. Currently it is not possible to do what you request. I am thinking to address this in a future version (hopefully 0.9). The idea will be to be able to display some posts on pages in all languages.

    But you will have to wait for the end of summer.

  7. renemorin
    Member
    Posted 1 year ago #

    Okay I tested it again and here's precisely what happens.

    Before implementation: One post on the French home page, no post on English home page. Pages are accessible from the menu, but they are not shown on the home pages (i.e. only posts are shown on the home pages).

    After implementation: Pages now appear in the posts areaon the home pages, in the order in which they were published, with French pages displayed on the English home page when no translation is available (plus, for some reason, a draft post included in the mix on the English home page).

    I'll be happy to test anything you send my way to fix this and report back to you in detail.

    Cheers,

    René

  8. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    I reproduce! Please replace the query_posts line by:

    query_posts(array_merge($wp_query->query, array('post_type' => 'post', 'lang' => 'fr'))); ?>
  9. renemorin
    Member
    Posted 1 year ago #

    Works beautifully! Thanks Chouby :-)

  10. fokkezb
    Member
    Posted 1 year ago #

    The solution above assumes that all posts at least have a version in the default language. However, this might not always be the case. For example; I have a blog where some posts are in English only, others in Dutch only and some in both. On the English blog I want to show English only, but on the Dutch blog I want to show English + Dutch.

    For this I use the following code. For the Dutch blog it selects both English and Dutch blogs and then just skips any English post for which it also finds a translation.

    <?
    global $wp_query;
    $language = pll_current_language();
    query_posts(array_merge($wp_query->query, array('post_type' => 'post', 'lang' => ($language == 'en' ? 'en' : 'en,nl'))));
    ?>
    
    <?php if (have_posts()) : $countposts = 0; while (have_posts()) : the_post(); ?>	
    
    	<?php
    	global $post;
    	if ($language == 'nl' && ($post_id = pll_get_post($post->ID, pll_current_language())) && $post_id != $post->ID && ($post_nl = get_post($post_id)) && $post_nl->post_status == 'published') {
    		continue;
    	}
    	?>
    
    	YOUR LOOP MARKUP HERE
    
    <? endif: ?>
  11. panotar
    Member
    Posted 1 year ago #

    Hey fokkezb,

    your modification looks like what I need. I'm running a german blog with the occasional english post in between. I want to display all posts by default on the 'german' page, and only the english ones (which are original post, no translations) behind the language switch.

    However, I'm not able to reproduce your example. I am putting this block

    <?
    global $wp_query;
    $language = pll_current_language();
    query_posts(array_merge($wp_query->query, array('post_type' => 'post', 'lang' => ($language == 'en' ? 'en' : 'en,de')))); ?>

    right before the 'have_posts', resulting in this line: query, array('post_type' => 'post', 'lang' => ($language == 'en' ? 'en' : 'en,de')))); ?>

    being written on top of my pages in the frontend. :(

    This is propably easy for people with programming skills but hard for me :) Any help is much appreciated.

    Thanx, panotar

  12. elviselvis
    Member
    Posted 1 year ago #

    Hello. I'm using Twenty Twelve theme. My website is in 5 languages, the default language is english. If there is no translation, for the page, it should show the default language page. Can you please show me how can I do it?

  13. Mte90
    Member
    Posted 1 year ago #

    but add an option in polylang for do this?

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags