我有一个自定义的帖子类型-在一个页面上,它显示了帖子类型的各种信息。总共有10个职位。在信息下方,我有一个幻灯片,作为显示其他帖子的循环的一部分。然而,这总是按字母顺序排列的。
我希望能够识别当前的单个页面,并将其显示为循环中的第一个条目。循环使用WP\\u查询,因此我需要找出一种方法以这种方式对其进行排序。
我怎样才能做到这一点?它是否涉及元查询?
例如:如果当前帖子是Post7,则按Post7、Post1、Post2、Post3等排序。
我有一个自定义的帖子类型-在一个页面上,它显示了帖子类型的各种信息。总共有10个职位。在信息下方,我有一个幻灯片,作为显示其他帖子的循环的一部分。然而,这总是按字母顺序排列的。
我希望能够识别当前的单个页面,并将其显示为循环中的第一个条目。循环使用WP\\u查询,因此我需要找出一种方法以这种方式对其进行排序。
我怎样才能做到这一点?它是否涉及元查询?
例如:如果当前帖子是Post7,则按Post7、Post1、Post2、Post3等排序。
您很可能可以重用该循环并运行两次。第一次显示查询的帖子,第二次显示其余帖子
// Get the current post id being viewed
$current_post_id = get_queried_object_id();
// Define your query and query arguments
$args = [
// All your arguments
];
$q = new WP_Query( $args );
if ( $q->have_posts() ) {
// Run the loop for the first time to display the post equal to the queried one
while ( $q->have_posts() ) {
$q->the_post();
if ( get_the_ID() == $current_post_id ) {
// Display the post equal to the queried one
}
} // endwhile
// Rewind the loop so that we can run it again
$q->rewind_posts();
// Run the loop for the second time to display all the other posts
while ( $q->have_posts() ) {
$q->the_post();
if ( get_the_ID() != $current_post_id ) {
// Display all the other posts skipping the queried one
}
} // endwhile
// Clean after ourselves, good housekeeping
wp_reset_postdata();
} // endif
我正在编写一个自定义帖子插件,它将自定义帖子分组显示为选项卡。每组4个岗位。是否可以编写一个偏移量随每次循环而增加的查询?因此,结果将是:-第一个查询显示从1到4的帖子-第二个查询显示从5到8的帖子-第三个查询显示从9到12的帖子等。 <div class=\"official-matters-tabs\"> <?php $args = array(\'post_type\' => \'official-matters\', \'showp