WordPress Global Variable Scope Issue with Infinite Scroll (JetPack)

I’m hoping someone has solved this. Can’t find a solution in the archive…

I’m using a WordPress global variable (I know, I know), called ‘ad_section’ to help with integrating DFP slots and ad units properly to different parts of a wordpress page based on content. It works as expected.

Read More

Except… I’m also using Infinite Scroll, which works fine to load posts.

But I’m running into one problem with JetPack’s Infinite Scroll. When Infinite Scroll loads the next set of posts, the template it uses to format subsequent posts (content.php) doesn’t have access to the global variable ‘ad_section’ anymore…. Works on page 1 but not page 2. Which means I can’t pull the correct ad unit in to that part of the page…

It has to be a scope thing. Would love a hand debugging it. Anyone solve this issue already?

Thanks!

Related posts

1 comment

  1. Solved this issue myself.

    Infinite Scroll starts its own WordPress session, and it’s as if the rest of the page never loaded. So even if you have a global variable created in another part of your template, it’s just not there for Infinite Scroll.

    So, to get around it, you need to re-create the logic that assigned the variable in the other part of the page, and add it to content.php (or whatever template you’re using to load Infinite Scroll pages)… It’s a bit of code duplication.

Comments are closed.