Fix multilingual post linking
This commit is contained in:
@@ -5,7 +5,7 @@
|
|||||||
* The SportsPress WPML class handles all WPML-related localization hooks.
|
* The SportsPress WPML class handles all WPML-related localization hooks.
|
||||||
*
|
*
|
||||||
* @class SP_WPML
|
* @class SP_WPML
|
||||||
* @version 1.1.9
|
* @version 1.3
|
||||||
* @package SportsPress/Classes
|
* @package SportsPress/Classes
|
||||||
* @category Class
|
* @category Class
|
||||||
* @author ThemeBoy
|
* @author ThemeBoy
|
||||||
@@ -25,39 +25,41 @@ class SP_WPML {
|
|||||||
*/
|
*/
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
add_filter( 'the_title', array( $this, 'the_title' ), 5, 2 );
|
add_filter( 'the_title', array( $this, 'the_title' ), 5, 2 );
|
||||||
add_filter( 'post_type_link', array( $this, 'post_type_link' ), 5, 2 );
|
add_filter( 'post_type_link', array( $this, 'post_type_link' ), 5, 3 );
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function the_title( $title, $id = null ) {
|
public static function the_title( $title, $id = null ) {
|
||||||
if ( self::can_localize( $id, get_post_type( $id ) ) ):
|
if ( self::can_localize( $id, $id ) ):
|
||||||
// Get translated post ID
|
// Get translated post ID
|
||||||
$translated_id = icl_object_id( $id, 'any', false, ICL_LANGUAGE_CODE );
|
$translated_id = icl_object_id( $id, 'any', false, ICL_LANGUAGE_CODE );
|
||||||
|
|
||||||
if ( $translated_id ):
|
if ( $translated_id ):
|
||||||
$post = get_post( $translated_id );
|
$post = get_post( $translated_id );
|
||||||
$title = $post->post_title;
|
if ( $post ) $title = $post->post_title;
|
||||||
endif;
|
endif;
|
||||||
endif;
|
endif;
|
||||||
|
|
||||||
return $title;
|
return $title;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function post_type_link( $url, $post ) {
|
public static function post_type_link( $url, $post, $leavename ) {
|
||||||
if ( self::can_localize( $post->ID, $post->post_type ) ):
|
if ( self::can_localize( $post ) ):
|
||||||
// Get translated post ID
|
// Get post ID
|
||||||
$translated_id = icl_object_id( $post->ID, 'any', false, ICL_LANGUAGE_CODE );
|
$id = $post->ID;
|
||||||
|
|
||||||
if ( $translated_id ):
|
// Get translated post ID
|
||||||
$url .= '?lang=ja';
|
$translated_id = icl_object_id( $id, 'any', false, ICL_LANGUAGE_CODE );
|
||||||
//$url = get_permalink( $translated_id );
|
|
||||||
|
if ( $translated_id && $translated_id != $id ):
|
||||||
|
return get_permalink( $translated_id, $leavename );
|
||||||
endif;
|
endif;
|
||||||
endif;
|
endif;
|
||||||
|
|
||||||
return $url;
|
return $url;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function can_localize( $id, $post_type ) {
|
public static function can_localize( $post, $id = null ) {
|
||||||
return ( function_exists( 'icl_object_id' ) && is_sp_post_type( $post_type ) && $id != get_the_ID() );
|
return function_exists( 'icl_object_id' ) && is_sp_post_type( get_post_type( $post ) ) && $id != get_the_ID();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user