• Resolved elviselvis

    (@elviselvis)


    Hello, I’m new to wordpress and trying to make my own theme on example of Twenty twelve theme.

    In my theme, I have several places where I need to show different posts, I achieved it simply by

    <?php
    $post_id = 26;
    $queried_post = get_post($post_id);
    echo $queried_post->post_content;
    ?>

    So far so good, but the problem is that my website is multilingual, I’m using Polylang plugin for that. So obviously posts won’t change to translated one if language is switched.
    Maybe someone can give me advice how to make it the right way, so that on correct language the correct post is showed?

Viewing 4 replies - 1 through 4 (of 4 total)
  • <?php
    $post_id = 26;
    if (function_exists('pll_get_post'))
      $post_id = pll_get_post($post_id);
    $queried_post = get_post($post_id);
    echo $queried_post->post_content;
    ?>

    The instruction I added will test that Polylang is activated and if so get the tranlation in the correct language.

    Otherwise, you can always do something like:

    $language = get_locale();
    if ($language == 'en_US') {
     ... do something ...
    }
    else {
     ... do something else ...
    }

    Thread Starter elviselvis

    (@elviselvis)

    Thank You very much! Exactly what I was looking for. Everything works. ??
    Another small thing, if there is no translation, how to show the default post with post_id 26?

    Change the code to this:

    <?php
    $post_id = 26;
    if (function_exists('pll_get_post') && ($tr_id = pll_get_post($post_id)) && !empty($tr_id))
      $post_id = pll_get_post($post_id);
    $queried_post = get_post($post_id);
    echo $queried_post->post_content;
    ?>

    Thread Starter elviselvis

    (@elviselvis)

    Thanks again! Works ??

    Love wordpress, tried lot of CMS’s, but wordpress is much easier and more flexible than any other CMS. Community is also greate, very helpful!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Show specific translated post in template regarding of the language’ is closed to new replies.