wpml - show all CPT at archive page, default language if not translated


/ Published in: PHP
Save to your folder(s)

shows list of cpt for example on archive page when wpml is installed. if particular post has no translation, it shows it in default language


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. // save current language
  4. $current_lang = apply_filters( 'wpml_current_language', null );
  5.  
  6. //get the default language
  7. $default_lang = apply_filters( 'wpml_default_language', null );
  8. //fetch posts in default language
  9. do_action( 'wpml_switch_language', $default_lang);
  10. //query args
  11. $custom_query_args = array(
  12. 'cat' => 1
  13. );
  14. //build query
  15. $custom_query = new wp_query($custom_query_args);
  16. //loop
  17. while ( $custom_query->have_posts() ) : $custom_query->the_post();
  18. //check if a translation exist
  19. $t_post_id = apply_filters( 'wpml_object_id', $post->ID, 'post', false, $current_lang );
  20. if(!is_null($t_post_id)){
  21. $t_post = get_post( $t_post_id);
  22. ?>
  23. <a href="<?php echo get_permalink($t_post_id); ?>" title="<?php echo get_the_title($t_post_id); ?>"><?php echo get_the_title($t_post_id); ?></a>
  24. <?php
  25. }
  26. //no translation? display default language
  27. else{ ?>
  28. <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
  29. <?php
  30. }
  31. endwhile;
  32. wp_reset_query();
  33. do_action( 'wpml_switch_language', $current_lang);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.