我没有得到任何东西,一页空白。我错过什么了吗?
是的,你是。
$query->the_post() 不显示任何内容-它只移动posts数组中的指针/键($query->posts) 并设置全局post数据($GLOBALS[\'post\']). 所以是的,作为对你答案的回应,the_content() 将用于在循环中显示当前帖子的内容。
但实际上,您可以简单地使用page_id parameter 如是,要查询page 类型并具有特定ID:
$args = array(
// Instead of using post_type and post__in, you could simply do:
\'page_id\' => 1149,
);
还有,你应该打电话
wp_reset_postdata() 循环结束后,模板标记(例如。
the_content() 和
the_title()) 将再次使用主查询的当前帖子。
while ($query->have_posts()) {
$query->the_post();
the_content();
}
wp_reset_postdata();
为了让你知道,你也可以
get_post() 和
setup_postdata() 就像这样,无需使用
new WP_Query() 方式:
//global $post; // uncomment if necessary
$post = get_post( 1149 );
setup_postdata( $post );
the_title();
the_content();
// ... etc.
wp_reset_postdata();