我的最终目标是列出最新的5篇帖子,不分类别。
我想先做一个WP_Query
获取最新的两篇文章并将其插入到特定的HTML结构中。
那么我想再做一个WP_Query
获取接下来的三篇文章,并将它们插入到不同的HTML结构中。
我尝试了一些代码片段,每一个都是WP_Query
从两个HTML结构中的最新帖子开始。是否有一个参数,我可以使用它专门告诉第二个查询跳过前两篇文章?
我的最终目标是列出最新的5篇帖子,不分类别。
我想先做一个WP_Query
获取最新的两篇文章并将其插入到特定的HTML结构中。
那么我想再做一个WP_Query
获取接下来的三篇文章,并将它们插入到不同的HTML结构中。
我尝试了一些代码片段,每一个都是WP_Query
从两个HTML结构中的最新帖子开始。是否有一个参数,我可以使用它专门告诉第二个查询跳过前两篇文章?
为post\\u per\\u page=2运行一个wp\\u查询,并获取数组中这2篇文章的ID,以便在接下来需要的3篇文章中排除
<?php
// The Query
$next_args = array(
\'post_type\' => \'<your_post_type>\',
\'post_status\' => \'publish\',
\'posts_per_page\'=>2,
\'order\'=>\'DESC\',
\'orderby\'=>\'ID\',
);
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
$not_in_next_three = array();
while ( $the_query->have_posts() ) {
$the_query->the_post();
//your html here for latest 2
$not_in_next_three[] = get_the_ID();
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
现在,在获取接下来3篇文章的wp\\u查询中排除上面创建的数组// The Query
$next_args = array(
\'post_type\' => \'<your_post_type>\',
\'post_status\' => \'publish\',
\'posts_per_page\'=>3,
\'order\'=>\'DESC\',
\'orderby\'=>\'ID\',
\'post__not_in\'=>$not_in_next_three
);
$next_the_query = new WP_Query( $next_args );
// The Loop
if ( $next_the_query->have_posts() ) {
while ( $next_the_query->have_posts() ) {
$next_the_query->the_post();
//your html here fir latest next 3
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>
您可以使用WP_Query
\'s pagination paramters.
然而,使用两个查询来实现这一目标效率很低。更好的解决方案是使用一个查询并根据WP_Query
\'s $current_post
property, 它(在循环中使用时)反映当前结果页面中当前正在处理的帖子的索引。
我不熟悉PHP和WordPress,我很好奇:为什么WP查询是大写的?这是基于查询类型的PHP命名约定吗?还是怎样谢谢