Skip to content

Instantly share code, notes, and snippets.

@cyberwani
Created October 11, 2017 08:15
Show Gist options
  • Save cyberwani/6e22d55707c2328e08568dfff1bfe36c to your computer and use it in GitHub Desktop.
Save cyberwani/6e22d55707c2328e08568dfff1bfe36c to your computer and use it in GitHub Desktop.
Shortcode to display content based on current language.
<?php
if( ! function_exists('polylang_content_func') ){
function polylang_content_func( $atts, $content = "" ) {
$atts = shortcode_atts( array(
'lang' => '',
), $atts );
extract($atts);
// Bail early if no language provided
if( $lang == '' ) return;
$pll_current_language = pll_current_language();
// Bail early if current language is not same as provided language
if( $pll_current_language != $lang ) return;
return do_shortcode($content);
}
}
add_shortcode( 'polylang_content', 'polylang_content_func' );
@innerspace24-7
Copy link

Conditional Shortcode Based on Language ( for another chance to find it ...)
Wow! Thank you very much, this code runs just perfect!

(for those who are not that familiar with coding, like me :-) ):
Insert the above code into your functions.php of your child-theme.
Use the short code in your wordpress pages/widget etc. like this:

[polylang_content lang=en] Language is displayed in english [/polylang_content]
[polylang_content lang=de] Sprache wird in deutsch angezeigt [/polylang_content]

Great! Many thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment