我正在尝试构建一个经典的归档页面,每页10篇文章。但在经典循环的顶部会显示此存档中所有帖子的列表。
(分页的帖子将显示功能图片、摘录……而所有帖子的列表将只保留永久链接)
我一直在想,我可以使用已经处理过的全局循环,并在分页之前挂接以获取数据,然后让它分页。
但我不知道到目前为止有多远。
另一种解决方案是在页面上运行两个循环,一个有分页,另一个没有分页,但我认为第一种方法可能更好,也可能更好。。。。
你们有没有遇到过类似的问题?对这个问题有什么建议吗?
我正在尝试构建一个经典的归档页面,每页10篇文章。但在经典循环的顶部会显示此存档中所有帖子的列表。
(分页的帖子将显示功能图片、摘录……而所有帖子的列表将只保留永久链接)
我一直在想,我可以使用已经处理过的全局循环,并在分页之前挂接以获取数据,然后让它分页。
但我不知道到目前为止有多远。
另一种解决方案是在页面上运行两个循环,一个有分页,另一个没有分页,但我认为第一种方法可能更好,也可能更好。。。。
你们有没有遇到过类似的问题?对这个问题有什么建议吗?
如果您想在单个查询中完成它,那么钩子pre_get_posts
要查询所有帖子,请执行以下操作:
function wpd_archive_all_posts( $query ){
if( $query->is_post_type_archive( \'attractions\' ) && $query->is_main_query() )
$query->set( \'posts_per_page\', -1 );
}
add_action( \'pre_get_posts\', \'wpd_archive_all_posts\' );
然后在模板中手动分页这些结果:if( have_posts() ){
$posts_per_page = 10;
$paged = ( get_query_var(\'paged\') ) ? get_query_var(\'paged\') : 1;
$start = ( ( $paged - 1 ) * $posts_per_page ) - 1;
$end = ( $paged * $posts_per_page ) - 1;
$wp_query->current_post = $start;
while( have_posts() ){
the_post();
// output post data here
if( $end == $wp_query->current_post )
break;
}
}
然后倒带并输出所有内容:$wp_query->rewind_posts();
while( have_posts() ){
the_post();
the_title();
}
这里需要注意的是,第一个循环可能不会调用loop_end
动作,因为你可能在击中最后一根柱子之前就躲开了。实际上,这可能有副作用,也可能没有副作用,这取决于您是否有挂接该操作的插件。直接摘自WordPress codex:
// main loop
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
// rewind
<?php do something with query_posts() here to modify the loop here. ?>
<?php rewind_posts(); ?>
// new loop
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
注意,rewind\\u posts()就是这样做的,它将post循环倒回到开头,允许您再次循环。但是,如果您想要的是两个稍有不同的列表,即:第一个循环是100篇文章的列表,第二个循环是每页只有10篇文章的分页列表,那么您可以在再次运行查询之前使用query\\u posts()来更改查询。通常不建议使用query\\u posts(),因为它会改变主循环,但在这种情况下,这将是您的最佳选择。
我不确定这是不是最好的解决方案,而监视它所做的查询比标准存档页少?也许是因为我没有给meta打电话,比如作者和日期。。。如果有人有真正的答案请告诉我干杯
//first the main loop paginated:
if ( have_posts() ) :
while ( have_posts() ) : the_post();
//get permalink, title,custom metas and feature image
else :
get_template_part( \'content\', \'none\' );
endif;
//then I run the second loop for all posts of the Custom Post Type
$args = array(
\'post_type\' => \'attractions\',
\'post_status\' => \'publish\',
\'posts_per_page\'=>-1
);
$wp_query = new WP_Query($args);
while ( $wp_query->have_posts() ) : $wp_query->the_post();
//get datas of all posts
endwhile; wp_reset_postdata();
嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post