icl_link_to_element

The Polylang icl link to element function.

Description

icl_link_to_element( $id, (string) $type = 'post', (string) $text = '', (array) $args = array(), (string) $anchor = '', (constant) $echo = true, (bool) $return_original_if_missing = true ); 

Parameters (7)

0. $id
The id.
1. $type — Optional. (string) => 'post'
The type.
2. $text — Optional. (string) => ''
The text.
3. $args — Optional. (array) => array()
The args.
4. $anchor — Optional. (string) => ''
The anchor.
5. $echo — Optional. (constant) => true
The echo.
6. $return_original_if_missing — Optional. (bool) => true
The return original if missing.

Usage

  1. if ( !function_exists( 'icl_link_to_element' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'polylang/modules/wpml/wpml-legacy-api.php'; 
  3.  
  4. // The id. 
  5. $id = null; 
  6.  
  7. // The type. 
  8. $type = 'post'; 
  9.  
  10. // The text. 
  11. $text = ''; 
  12.  
  13. // The args. 
  14. $args = array(); 
  15.  
  16. // The anchor. 
  17. $anchor = ''; 
  18.  
  19. // The echo. 
  20. $echo = true; 
  21.  
  22. // The return original if missing. 
  23. $return_original_if_missing = true; 
  24.  
  25. // NOTICE! Understand what this does before running. 
  26. $result = icl_link_to_element($id, $type, $text, $args, $anchor, $echo, $return_original_if_missing); 
  27.  

Defined (1)

The function is defined in the following location(s).

/modules/wpml/wpml-legacy-api.php  
  1. function icl_link_to_element( $id, $type = 'post', $text = '', $args = array(), $anchor = '', $echo = true, $return_original_if_missing = true ) { 
  2. if ( 'tag' == $type ) { 
  3. $type = 'post_tag'; 
  4.  
  5. $pll_type = ( 'post' == $type || pll_is_translated_post_type( $type ) ) ? 'post' : ( 'term' == $type || pll_is_translated_taxonomy( $type ) ? 'term' : false ); 
  6. if ( $pll_type && ( $lang = pll_current_language() ) && ( $tr_id = PLL()->model->$pll_type->get_translation( $id, $lang ) ) && PLL()->links->current_user_can_read( $tr_id ) ) { 
  7. $id = $tr_id; 
  8. } elseif ( ! $return_original_if_missing ) { 
  9. return ''; 
  10.  
  11. if ( post_type_exists( $type ) ) { 
  12. $link = get_permalink( $id ); 
  13. if ( empty( $text ) ) { 
  14. $text = get_the_title( $id ); 
  15. } elseif ( taxonomy_exists( $type ) ) { 
  16. $link = wpcom_vip_get_term_link( $id, $type ); 
  17. if ( empty( $text ) && ( $term = get_term( $id, $type ) ) && ! empty( $term ) && ! is_wp_error( $term ) ) { 
  18. $text = $term->name; 
  19.  
  20. if ( empty( $link ) || is_wp_error( $link ) ) { 
  21. return ''; 
  22.  
  23. if ( ! empty( $args ) ) { 
  24. $link .= ( false === strpos( $link, '?' ) ? '?' : '&' ) . http_build_query( $args ); 
  25.  
  26. if ( ! empty( $anchor ) ) { 
  27. $link .= '#' . $anchor; 
  28.  
  29. $link = sprintf( '<a href="%s">%s</a>', esc_url( $link ), esc_html( $text ) ); 
  30.  
  31. if ( $echo ) { 
  32. echo $link; 
  33.  
  34. return $link;