Forums

[resolved] Custom fields not accessible in footer.php (6 posts)

  1. zhaval
    Member
    Posted 1 year ago #

    Hi,

    I am building a bilingual site (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.

  2. Ivovic
    Member
    Posted 1 year ago #

    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;
    ?>
  3. zhaval
    Member
    Posted 1 year ago #

    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!

  4. Ivovic
    Member
    Posted 1 year ago #

    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.

  5. zhaval
    Member
    Posted 1 year ago #

    Thanks! Hvala?!

  6. Ivovic
    Member
    Posted 1 year ago #

    Molim ;)

Topic Closed

This topic has been closed to new replies.

About this Topic