Get language of post from WPML | Only show certain language | Double Marvellous
Back to Blog
Development Oct 14, 2024 4 min read

Get language of post from WPML | Only show certain language

Get language of post from WPML | Only show certain language

Do you need to use the WPML plugin? Well if you do and you need to restrict something to the current language, here’s how:

//First, get the current language

<?php
 $my_current_lang = apply_filters( 'wpml_current_language', NULL );
?>

//Echoing this will give me en on my English language page

//Next, whilst in a loop of posts, get the language of each post:

<?php
$hotid = $blog_post->ID;
$post_language_information = apply_filters( 'wpml_post_language_details', NULL, $hotid );
$hotlang = $post_language_information["language_code"];
?>

//Here, in a loop, the variable $hotlang will give me de for a German //post, en for an English post, etc.

//To only show English posts, just check $hotlang against your main //language variable

<?php if($hotlang === $my_current_lang){ ?>

 <li>
  
    Your post content or whatever

 </li>

<?php } ?>

//hope that helps someone!

Double Marvellous

AI Chatbot

Double Marvellous.