Displaying only translated content
-
Hello,
How would it be possible to only display posts that have a translation.
When i switch languages i would only like to see the translated posts.Thank you
-
Hello,
I think the best way to do this is to add a condition into your wordpress loop, in your template files. Something like this:
while ( have_posts() ) : the_post(); global $sublanguage; if (empty($sublanguage) || $sublanguage->is_main() && $post->post_content || $sublanguage->is_sub() && $post->post_content != apply_filters( 'sublanguage_translate_post_field', $post->post_content, $post, 'post_content')) { // loop content here } endwhile;
Does it help you?
Im having trouble adding it into my code. Not that strong with php.
This is the code i am using at the moment
<div class="grid_wrapper"> <?php $args = array ( 'category_name' => 'Artiklid', 'posts_per_page' => 4, 'orderby' => 'date', ); ?> <?php $cat_posts = new WP_query($args) ?> <?php if ($cat_posts->have_posts()) : while ($cat_posts->have_posts()) : $cat_posts->the_post(); ?> <div class="post_preview card"> <h1><?php the_title(); ?></h1> <p class="publish_data"> <?php the_time('d.m.Y') ?> | <?php echo strip_tags(get_the_tag_list('',', ','')); ?> </p> <p><?php the_excerpt(); ?></p> <a class="btn waves-effect waves-light" href="<?php the_permalink() ?>"><?php echo get_the_title(166) ?></a> </div> <?php endwhile; endif?> <a href="artiklid" id="last" class="show_more_btn"><?php echo get_the_title(172) ?></a> </div>
Ok I inserted it in your code at the right place.
But if you are using a commercial theme (or anything but a custom one), please be careful with updates: next time you’ll upgrade your theme everything will be erased. To manage this you should use a child theme and put this code in the child theme file.
<div class="grid_wrapper"> <?php $args = array ( 'category_name' => 'Artiklid', 'posts_per_page' => 4, 'orderby' => 'date', ); global $sublanguage; ?> <?php $cat_posts = new WP_query($args) ?> <?php if ($cat_posts->have_posts()) : while ($cat_posts->have_posts()) : $cat_posts->the_post(); ?> <?php if (empty($sublanguage) || $sublanguage->is_main() && $cat_posts->post->post_content || $sublanguage->is_sub() && $cat_posts->post->post_content != apply_filters( 'sublanguage_translate_post_field', $cat_posts->post->post_content, $cat_posts->post, 'post_content')) { ?> <div class="post_preview card"> <h1><?php the_title(); ?></h1> <p class="publish_data"> <?php the_time('d.m.Y') ?> | <?php echo strip_tags(get_the_tag_list('',', ','')); ?> </p> <p><?php the_excerpt(); ?></p> <a class="btn waves-effect waves-light" href="<?php the_permalink() ?>"><?php echo get_the_title(166) ?></a> </div> <?php } ?> <?php endwhile; endif?> <a href="artiklid" id="last" class="show_more_btn"><?php echo get_the_title(172) ?></a> </div>
Thank you so much for the reply! It is doing the trick but not quite.
At first i didnt think it worked but after trying it out in another part of the template I can see what its doing.The code:
<div class="grid_wrapper"> <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $args = array ( 'category_name' => 'Artiklid', 'post_type' => 'post', 'posts_per_page' => 5, //showposts is deprecated 'paged' => $paged, 'orderby' => 'date', 'order' => DESC, ); global $sublanguage; ?> <?php $cat_posts = new WP_query($args) ?> <?php // globalize $wp_query global $wp_query; // copy $wp_query into a temporary variable $temp_wp_query = $wp_query; // nullify $wp_query $wp_query = null; // move $loop into $wp_query $wp_query = $cat_posts; ?> <?php if ($cat_posts->have_posts()) : while ($cat_posts->have_posts()) : $cat_posts->the_post(); ?> <?php if (empty($sublanguage) || $sublanguage->is_main() && $cat_posts->post->post_content || $sublanguage->is_sub() && $cat_posts->post->post_content != apply_filters( 'sublanguage_translate_post_field', $cat_posts->post->post_content, $cat_posts->post, 'post_content')) { ?> <?php $c++; if( $c == 1) :?> <div id="viimati_ilmunud_uudis" class="post_main card" style="margin-left:0px;"> <h1><?php the_title(); ?></h1> <p class="publish_data"> <?php the_time('d.m.Y') ?> | <?php echo strip_tags(get_the_tag_list('',', ','')); ?> </p> <p><?php the_excerpt(); ?></p> <a class="btn waves-effect waves-light" href="<?php the_permalink() ?>"><?php echo get_the_title(166) ?></a> </div> <div class="spacer"></div> <?php else :?> <div class="post_preview card"> <h1><?php the_title(); ?></h1> <p class="publish_data"> <?php the_time('d.m.Y') ?> | <?php echo strip_tags(get_the_tag_list('',', ','')); ?> </p> <p><?php the_excerpt(); ?></p> <a class="btn waves-effect waves-light" href="<?php the_permalink() ?>"><?php echo get_the_title(166) ?></a> </div> <?php endif;?> <?php } ?> <?php endwhile; endif ?> </div>
This part is paginated. All the non translated posts are removed but i have to click through the pagination to get to the translated ones. Until i get to the translated posts i see only the previous and next page links and nothing else.
Sorry, I forgot about pagination. Of course this method doesn’t work with pagination. Unfortunately I don’t have a work around for this ??
I’ll try to implement a way to do this in the future.
- The topic ‘Displaying only translated content’ is closed to new replies.