Thursday, August 28, 2014

Wordpress: Get the ID of a post in the current language with Polylang

If you know the ID of a content that you need a different language version of, you can get the ID of the current language version with this php code:

 if(pll_current_language() != pll_get_post_language($id)) {  
      $id = pll_get_post($id);  
 };  

My own very useful functions can be easily extended like this:

 function getPostTitle($id) {  
      if(pll_current_language() != pll_get_post_language($id)) {  
           $id = pll_get_post($id);  
      };  
      return get_the_title($id);  
 };  
 function getPostContent($id) {  
      if(pll_current_language() != pll_get_post_language($id)) {  
           $id = pll_get_post($id);  
      };  
      $post_object = get_post($id);    
      return do_shortcode( $post_object->post_content );  
 };  
 function getPostThumbnailUrl($id) {  
      if(pll_current_language() != pll_get_post_language($id)) {  
           $id = pll_get_post($id);  
      };  
      $thumb_id = get_post_thumbnail_id($id);  
      $thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true);  
      $thumb_url = $thumb_url_array[0];  
      return $thumb_url;  
 };