Posted By

kkarpieszuk on 11/06/13


Tagged

wordpress archive WPML CPT


Versions (?)

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


 / Published in: PHP
 

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

  1. <?php
  2. global $sitepress;
  3. // save current language
  4. $current_lang = $sitepress->get_current_language();
  5. //get the default language
  6. $default_lang = $sitepress->get_default_language();
  7. //fetch posts in default language
  8. $sitepress->switch_lang($default_lang);
  9. //query args
  10. $custom_query_args = array(
  11. 'cat' => 1
  12. );
  13. //build query
  14. $custom_query = new wp_query($custom_query_args);
  15. //loop
  16. while ( $custom_query->have_posts() ) : $custom_query->the_post();
  17. //check if a translation exist
  18. $t_post_id = icl_object_id($post->ID, 'post', false, $current_lang);
  19. if(!is_null($t_post_id)){
  20. $t_post = get_post( $t_post_id);
  21. ?>
  22. <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>
  23. <?php
  24. }
  25. //no translation? display default language
  26. else{ ?>
  27. <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
  28. <?php
  29. }
  30. endwhile;
  31. wp_reset_query();
  32. $sitepress->switch_lang($current_lang);

Report this snippet  

You need to login to post a comment.