How to: Display all posts of specified language in X theme
-
Credit goes to Child of WordPress for sharing how this is done. It will display all posts of a specified language. If a translation exists for a particular post, which matches the sites currently set language, that translation will be shown instead.
How to display posts only in one language in Polylang pluginI’m just posting here to share how I got this working, specifically in X theme (Integrity).
I copied _index.php from [parent-theme]/framework/views/global/_index.php to the same location in my child-theme.
I then edit the child-theme version and add two blocks of code, taken from Child of WordPress’ guide.
This goes before the post loop. Specify here what you want the default language to be, by changing ‘en’ (English) to what you want.
<?php query_posts(array('post_type' => 'post','lang' => 'en')); ?>
This goes inside the post loop.
<?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); }?>
The whole file now looks like this:
<?php // ============================================================================= // VIEWS/GLOBAL/_INDEX.PHP // ----------------------------------------------------------------------------- // Includes the index output. // ============================================================================= $stack = x_get_stack(); if ( is_home() ) : $style = x_get_option( 'x_blog_style', 'standard' ); $cols = x_get_option( 'x_blog_masonry_columns', '2' ); $condition = is_home() && $style == 'masonry'; elseif ( is_archive() ) : $style = x_get_option( 'x_archive_style', 'standard' ); $cols = x_get_option( 'x_archive_masonry_columns', '2' ); $condition = is_archive() && $style == 'masonry'; elseif ( is_search() ) : $condition = false; endif; ?> <?php if ( $condition ) : ?> <?php x_get_view( 'global', '_script', 'isotope-index' ); ?> <div id="x-iso-container" class="x-iso-container x-iso-container-posts cols-<?php echo $cols; ?>"> <?php query_posts(array('post_type' => 'post','lang' => 'en')); ?> <?php 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); }?> <?php if ( $stack != 'ethos' ) : ?> <?php x_get_view( $stack, 'content', get_post_format() ); ?> <?php else : ?> <?php x_ethos_entry_cover( 'main-content' ); ?> <?php endif; ?> <?php endwhile; ?> <?php else : ?> <?php x_get_view( 'global', '_content-none' ); ?> <?php endif; ?> </div> <?php else : ?> <?php if ( have_posts() ) : ?> <?php while ( have_posts() ) : the_post(); ?> <?php x_get_view( $stack, 'content', get_post_format() ); ?> <?php endwhile; ?> <?php else : ?> <?php x_get_view( 'global', '_content-none' ); ?> <?php endif; ?> <?php endif; ?> <?php pagenavi(); ?>
- The topic ‘How to: Display all posts of specified language in X theme’ is closed to new replies.