Last active
July 11, 2022 02:26
-
-
Save audrasjb/ff620b758bd21dcdfe4870273282327e to your computer and use it in GitHub Desktop.
Multilingual blog with polylang: query posts in all languages in but only show one version if a post has multiple translations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* 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' ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Assuming we are using a TwentyNineteen child theme. | |
*/ | |
<?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 post_class(); echo $lang_attribute; ?>> | |
<header class="entry-header"> | |
<?php the_title( sprintf( '<h2 class="entry-title">'. $lang_title .'<a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' ); ?> | |
</header><!-- .entry-header --> | |
<div class="entry-content excerpt-home"> | |
<?php the_excerpt(); ?> | |
</div><!-- .entry-content --> | |
<footer class="entry-footer"> | |
<?php twentynineteen_entry_footer(); ?> | |
</footer><!-- .entry-footer --> | |
</article><!-- #post-<?php the_ID(); ?> --> | |
<?php endwhile; ?> | |
<?php twentynineteen_the_posts_navigation(); ?> | |
<?php endif; ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment