Book a Free Call

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!

Need rapid website development:

For smaller projects at high speed


  • Adding script tag embed to Gatsby component

    Read More
  • Get more than 100 queries in GraphQL (I felt stupid)

    Read More
  • Animate on Scroll (AOS) into Nuxt js

    Read More
  • WordPress Super-admin hack

    Read More
  • Add content or ACF field under thumbnail/image gallery in Woocommerce

    Read More

Tags


Categories

Is it urgent? Donate to Cork Simon

Got an urgent issue?

Charlie's Cottage, Carrigrohane, Co. Cork, T12 WY9H

(085) 7686677