-
-
Save andreasciamanna/02efa4ff0836b253ee36 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function wpml_the_content_filter( $content ) { | |
global $post, $sitepress; | |
$element_type = 'post_' . $post->post_type; | |
$trid = $sitepress->get_element_trid( $post->ID, 'post_' . $post->post_type ); | |
$element_translations = apply_filters( 'wpml_get_element_translations', null, $trid, $element_type ); | |
$translation_type = apply_filters( 'wpml_element_translation_type', null, $post->ID, $post->post_type ); | |
if ( WPML_ELEMENT_IS_NOT_TRANSLATED !== $translation_type ) { | |
$translations = ''; | |
$duplicates = ''; | |
if ( 0 < count( $element_translations ) ) { | |
foreach ( $element_translations as $language_code => $element_translation ) { | |
if ( (int) $element_translation->element_id !== $post->ID ) { | |
$element_translation_type = apply_filters( 'wpml_element_translation_type', null, $element_translation->element_id, $post->post_type ); | |
$element_language = apply_filters( 'wpml_post_language_details', null, $element_translation->element_id ); | |
$translation_url = get_permalink( $element_translation->element_id ); | |
$translation_url = $sitepress->convert_url( $translation_url, $language_code ); | |
$element_link = '<a href="' . esc_url( $translation_url ) . '">' . $element_language['display_name'] . '</a>'; | |
if ( WPML_ELEMENT_IS_TRANSLATED === $element_translation_type || (bool) $element_translation->original ) { | |
$translations .= '<li>'; | |
$translations .= $element_link; | |
$translations .= '</li>'; | |
} | |
if ( WPML_ELEMENT_IS_A_DUPLICATE === $element_translation_type ) { | |
$duplicates .= '<li>'; | |
$duplicates .= $element_link; | |
$duplicates .= '</li>'; | |
} | |
} | |
} | |
} | |
if ( '' !== $translations ) { | |
$content .= '<p>This post is also available in: </p>'; | |
$content .= '<ul>'; | |
$content .= $translations; | |
$content .= '</ul>'; | |
} | |
if ( '' !== $duplicates ) { | |
$content .= '<p>This post is duplicated in: </p>'; | |
$content .= '<ul>'; | |
$content .= $duplicates; | |
$content .= '</ul>'; | |
} | |
} | |
return $content; | |
} | |
add_filter( 'the_content', 'wpml_the_content_filter' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment