$args and Queries and template parts

I have a wordpress template part called “grid-loop.php” that has a query that shows a grid of items. Initially, in this template part, I pass $args to it. It works fine. I’m trying to set it up differently now. Let me explain.

I now have a page template (page-news.php) that contains the args:

	$args = array(
	'category_name' => 'news',
); ?>

Beneath that I have this:

<?php get_template_part( 'grid', 'loop' ); ?>

The grid-loop contains this:

	$the_query = new WP_Query( $args );

So basically the $args is in a separate document from the actual WP_Query. It doesn’t work. Maybe it can’t work this way or I’m doing something wrong? When I put the arguments with the query in the same document, it works fine. Suggestions?

Looking at http://codex.wordpress.org/Function_Reference/get_template_part, and your code below:

<?php get_template_part( 'grid', 'loop' ); ?>

it is looking for a file called grid-loop.php. However your file is called page-news.php, so this is probably your issue.

Actually page-news.php loads the template part grid-loop.php


<?php get_header(); ?>
<!-- News Page -->
<section class="grid">
	<!-- Grid -->
	<?php get_template_part( 'grid', 'loop' ); ?>


<?php get_footer(); ?>

My question is about whether I can setup the variable “$args” within page-news.php but have grid-loop.php load the variable. Does that make sense?

For example do I need something like Post and Get? Of which I don’t entirely know how to use yet.

Take a look at the page I linked to. If your two files are in the same directory, then it seems like it will get it fine.

Well, I haven’t had problems using the template parts before. This particular task is not working and I suppose I haven’t got a clue. Strange.