假设您希望保持WP\\u查询完全相同,我可以想出两种方法,使用各种循环来实现这一点。
1) 从posts
大堆
WP\\u查询有一组properties and methods 其中之一是posts
大堆
if( $the_query->have_posts() ) {
// Grab the first post object
$single_post = $the_query->posts[0]; // $post is reserved by global.
// This only gives you access to object properties, not `the_*()` functions.
echo $singe_post->post_title;
}
// Overwrite the global $post object
if( $the_query->have_posts() ) {
// Grab the global post
global $post;
$post = $the_query->posts[0]; // Grab the first post object.
setup_postdata( $post ); // Allows you to use `the_*()` functions. Overwrites global $post
the_title();
// Reset global $post back to original state
wp_reset_postdata();
}
2)必要时回放查询。
以下内容将正常循环,但最后我们将倒带它。这确保了如果我们想循环浏览所有帖子,它将包括第一篇帖子。此方法使用WP\\u Query::rewind\\u posts
if( $the_query->have_posts() ) {
while( $the_query->have_posts() ) {
$the_query->the_post();
// Break out of the loop after displaying 1 post.
if( $the_query->current_post >= 1 ) {
break;
}
the_title();
}
// Rewind the posts back to index 0, or the beginning.
$the_query->rewind_posts();
// Always call after running a secondary query loop that uses `the_post()`
the_query->wp_reset_postdata();
}