我正在制作一个单页网站。在这个页面上,我想运行三到四次WP\\u Query来拉入这3-4个页面。
页面看起来有点像这样:
<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>
当然,我在这里重复我自己的话,这有点浪费。我要做的是对WP\\u Query的这个实例进行函数化。这是我所做的,但它似乎没有任何回报。它一定是很愚蠢的东西,但我看不出它可能是什么(在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();
}
当然,在我的模板文件中,我希望能够运行:<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>
欢迎咨询!谢谢