• Resolved zhaval

    (@zhaval)


    Hi,

    I am building a bilingual site (https://www.pedkl.si) and using a custom variable to determine which language the post / page is in. If I use this code

    <?php $key=”language”; echo get_post_meta($post->ID, $key, true); ?>

    in the header.php or sidebar.php, it returns the right thing. But if I use it in footer.php, nothing is displayed.

    I would be grateful for any hints on how to solve this. Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • well, that’s because it’s outside the loop.

    in your page template, do something like….

    <?php
      global $current_language;
      $current_language = get_post_meta($post->ID, 'language', true);
      echo $current_language;
    ?>

    then in the footer:

    <?php
      global $current_language;
      echo $current_langauge;
    ?>
    Thread Starter zhaval

    (@zhaval)

    Thank you, Ivovic, you solved it. I did try something similar before, but did not use the

    <?php global $current_language; ?>

    in the footer again — I wonder why this is necessary, seems kind of redundant.

    Have a great day!

    variable scope in php is a little different to what you might be accustomed to.

    think of the global declaration as “pulling” the global variable into the current scope, so it needs to be done each time you want to access a global var in a different scope.

    Thread Starter zhaval

    (@zhaval)

    Thanks! Hvala?!

    Molim ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Custom fields not accessible in footer.php’ is closed to new replies.