Skip to content
Advertisement

WordPress: passing $query to separate template file

I have the following code:

homepage.php

JavaScript

loop-feed-work.php

JavaScript

But when I view my homepage, I get the following error:

Fatal error: Uncaught Error: Call to a member function have_posts() on null in ******/loop-feed-work.php:1

Could this be because the query is in a different template file?

Advertisement

Answer

That’s exactly the problem. Unfortunatly get_template_part doens’t work like an include so you will lose the scope variable when inside the included file. The solution to that problem while mantaining the same logic is to change get_template_part and use:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement