Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save andreasciamanna/02efa4ff0836b253ee36 to your computer and use it in GitHub Desktop.
Save andreasciamanna/02efa4ff0836b253ee36 to your computer and use it in GitHub Desktop.
<?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