Jump to content
Sign in to follow this  
markmeeker

Pass PHP variables outside of Post Loop

Recommended Posts

markmeeker

There are a few variables I define inside the Post Loop (template.postloop.php, just before "pagelines_loop_post_end"). For example:

$post_category_id = get_the_category();

However these variables cannot be accessed outside of the Post Loop, and they can only be defined inside the loop from my understanding. Is there a way to access these variables from outside the loop, e.g. from footer.php? Thanks!

Share this post


Link to post
Share on other sites
timlinson

I think you'd have to use the `global` declaration on your vars.

Share this post


Link to post
Share on other sites
markmeeker

Hi Tim, I have tried using PHP globals in footer.php. For example:

if ($GLOBALS["post_category_id"] == 1)

Is this what you were referring to? Thanks for your help.

Share this post


Link to post
Share on other sites
timlinson
There are a few variables I define inside the Post Loop
I meant when you define these vars, they need to be declared as global if you're wanting to use them elsewhere: `global $myvar = 1;`

Share this post


Link to post
Share on other sites
markmeeker

That does the trick. However, note:

global $myvar = 1;
results in a parse error. It should be:
global $myvar;  $myvar = 1;

Thank you!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

×