Home › Forums › Support › Show posts in their original language if there is no translation available
- This topic has 1 reply, 2 voices, and was last updated 3 years, 6 months ago by
Leo.
-
AuthorPosts
-
February 6, 2022 at 1:39 pm #2107447
Leo
I use the Polylang plugin, and my site is available in three languages. I have three blogs on my site, one for each language, and each blog shows only the posts written in its language.
The problem is that some posts are available in two or three languages, while others are just in their original language.
I want to show all posts in each blog: the posts that have been translated as well as the ones with no translation available.
I found a way to do that. It requires two snippets, one in function.php and the other in home.php as explained here:
So, I added this first snippet with the Code Snippets plugin:
* * Query all posts versions on home page. */ function jba_modify_home_query( $query ) { if ( function_exists( 'pll__' ) && ! is_admin() && is_home() ) { $query->set('tax_query', ''); $query->set('lang', ''); } } add_action( 'pre_get_posts', 'jba_modify_home_query' );
All posts are now showing on the three blogs, but the issue is that some of them appear in two or three languages. This filter is supposed to be done by the second snippet. I added it with Elements and hooked it to wp_head. (In “Display Rules” I selected “Blog”):
<?php if ( have_posts() ) : ?> <?php while ( have_posts() ) : the_post(); ?> <?php if ( pll_get_post( get_the_ID() ) && get_the_ID() !== pll_get_post( get_the_ID() ) ) { // Do not display other languages posts if a translation in the current language exists. continue; } $lang_attribute = ''; $lang_title = ''; $lang_slug = pll_get_post_language( get_the_ID(), 'slug' ); $lang_name = pll_get_post_language( get_the_ID(), 'name' ); if ( pll_current_language() !== $lang_slug ) { $lang_attribute = 'lang="' . $lang_slug . '"'; $lang_title = '<span aria-label="(' . $lang_name . ')">[' . strtoupper( $lang_slug ) . ']</span> '; } ?> <article id="post-<?php the_ID(); ?>" <?php echo $lang_attribute; ?>> <h2 class="entry-title"> <a href="<?php the_permalink(); ?>"> <?php echo $lang_title; ?><?php the_title(); ?> </a> </h2> <div class="entry-content excerpt-home"> <?php the_excerpt(); ?> </div> </article> <?php endwhile; ?> <?php endif; ?>
It didn’t work, and besides, it shows a line of text at the top of the blog. You can see it here:
https://www.ruskus-patruskus.com/blog/
I know this probably exceeds your support service, but I have no idea what to do next. I’d be very grateful if you could at least point me the right way to go.
Many thanks! 🙂
February 6, 2022 at 1:46 pm #2107450Leo
StaffCustomer SupportHi Leo 🙂
Unfortunately I can’t recall that we have a solution for this.
I’d think your best bet is to check with Polylang’s support team to see if they are able to comment or point you in the right direction.
You can also try posting in a general WordPress forum like this one to get more audience:
https://wordpress.stackexchange.com/ -
AuthorPosts
- You must be logged in to reply to this topic.