Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I look up a custom post field value in header.php, and store it in a global variable.

In most of my template files, the includes go like this:

include header

...do a bunch of stuff

include sidebar
include footer

I can not persist the variable into the footer.php file...

is there any way to do this so that I can avoid having to call back to the database again to look up the custom field value again?

share|improve this question
    
This is a PHP question, not a WordPress question. –  Chip Bennett Feb 26 '12 at 2:29
add comment

2 Answers

up vote 2 down vote accepted

You'd have to declare it first above those includes and globalize it within header or footer before getting or setting the value.

however- in the specific context you're speaking of- getting a custom field value, it's only retrieved from the database on the first call, then cached, so subsequent calls won't hit the database again.

share|improve this answer
add comment

You can...

Create a function in functions.php file that asks for the custom field and then outputs it... then you need not include it in the header but can just use it wherever you want...

I assume you know how to retrieve the custom field value, but in any case here is an example from the wordpress codex:

echo get_post_meta( $post->ID, 'mykey', true );

I hope its a good solution if I understand you correct.

Cheers, Sagive.

share|improve this answer
    
Thanks for the response... this does not achieve the purpose. The problem is, even if I did as you suggested, I still need to make a call each time to the function, which in turn, makes a call to the db to get the value, right? I want to load the value into the variable the first time, and have it persist until the end of the footer.php –  robarwebservices Feb 26 '12 at 0:28
    
that would mean (as far as i know) either creating a session or send it between pages using the url (which is complecated in wordpress)... in any case, a custom field value should be drown from the database... i dont know why people are fearfull to use the DB.. thats why its their :) - its a small query - no harm.. –  Sagive SEO Feb 26 '12 at 1:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.