I'm making a one page site. On the page I want to run WP_Query three or four times to pull in those 3-4 pages.
The page looks a bit like this:
<div class="row">
<?php
$args = array( 'pagename' => 'page-1');
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
get_template_part('content');
endwhile;
endif;
// Reset Post Data
wp_reset_postdata();
?>
</div>
<div class="row">
<?php
$args = array( 'pagename' => 'page-2');
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
get_template_part('content');
endwhile;
endif;
// Reset Post Data
wp_reset_postdata();
?>
</div>
<div class="row">
<?php
$args = array( 'pagename' => 'page-3');
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
get_template_part('content');
endwhile;
endif;
// Reset Post Data
wp_reset_postdata();
?>
</div>
Of course I'm repeating myself here, and that's a bit of a waste. What I want to do is function-ize this instance of WP_Query. Here's what I've done, but it doesn't seem to return anything at all. It has to be something stupid, but I can't see what it could be (in functions.php):
/**
* Functionize wp_query
*
* @since 0.1
*/
function waterstreet_fetch_page( $pagename ){
$args = array( 'pagename' => $pagename );
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
get_template_part('content');
endwhile;
endif;
// Reset Post Data
wp_reset_postdata();
}
Then of course, in my template files, I want to be able to just run:
<div class="row">
<?php waterstreet_fetch_page('page-1'); ?>
</div>
<div class="row">
<?php waterstreet_fetch_page('page-2'); ?>
</div>
<div class="row">
<?php waterstreet_fetch_page('page-3'); ?>
</div>
Advice welcome! Thanks.