You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress如何在底部循环中跳过最新4篇文章?Offset属性无效

如何让WordPress循环跳过最新4篇文章?

嘿,我来帮你搞定这个问题!你想要在底部的文章循环里跳过最新的4篇文章,之前尝试用offset属性没达到预期效果,大概率是参数的使用方式或者查询逻辑的问题。

先看你的现有代码,你用了query_posts('posts_per_page=2')来控制每页显示2篇,但没加上跳过最新文章的参数。下面给你两种可行的解决方案:

方案1:直接修改query_posts的参数

最简单的方式就是在query_posts的参数里加上offset=4,这样就能直接跳过最新的4篇文章,只获取后面的内容。修改后的完整代码如下:

<?php if ( have_posts() ) : ?> 
<?php query_posts('posts_per_page=2&offset=4'); ?> 
<?php while ( have_posts() ) : the_post(); ?> 
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>> 
<div class="col-md-12" > 
<div class="res"> 
<img src="<?php the_post_thumbnail_url(array(200,200) ); ?> " alt="Norway" style="width:100%;"> 
<div class="bottom-left"><h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2></div> 
<div class="bottom"><h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2></div> 
<div class="top-left"> 
<?php foreach((get_the_category()) as $category){ $cat_link = get_category_link($category->cat_ID); echo '<a href="'.$cat_link.'"> '.$category-> name.'</a>'; } ?> 
</div> 
</div> 
<?php wp_link_pages(); ?> 
<!--. entry--> 
</div> 
</div><!-- .post--> 
<?php endwhile; /* rewind or continue if all posts have been fetched */ ?> 
<?php else : ?> 
<?php endif; ?></div>

方案2:使用WP_Query创建独立循环(更推荐)

query_posts会修改WordPress的主查询,可能会导致后续的循环或查询出现问题。更规范的做法是用WP_Query创建一个完全独立的自定义循环,这样不会影响主查询的内容。代码示例如下:

<?php
// 定义查询参数:跳过最新4篇,获取2篇
$custom_loop_args = array(
    'posts_per_page' => 2,
    'offset' => 4
);
// 初始化自定义查询
$custom_loop = new WP_Query($custom_loop_args);

// 开始循环
if ($custom_loop->have_posts()) :
    while ($custom_loop->have_posts()) : $custom_loop->the_post();
?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>> 
<div class="col-md-12" > 
<div class="res"> 
<img src="<?php the_post_thumbnail_url(array(200,200) ); ?> " alt="Norway" style="width:100%;"> 
<div class="bottom-left"><h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2></div> 
<div class="bottom"><h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2></div> 
<div class="top-left"> 
<?php foreach((get_the_category()) as $category){ $cat_link = get_category_link($category->cat_ID); echo '<a href="'.$cat_link.'"> '.$category-> name.'</a>'; } ?> 
</div> 
</div> 
<?php wp_link_pages(); ?> 
<!--. entry--> 
</div> 
</div><!-- .post-->
<?php
    endwhile;
    // 重置查询,恢复主查询的上下文
    wp_reset_postdata();
else :
    // 如果没有文章,这里可以添加提示内容
    echo '<p>No posts found.</p>';
endif;
?>

注意事项

  • 如果你的网站有分页功能,offset参数可能会和分页逻辑冲突,不过你这个是底部的固定循环,应该不会有这个问题。
  • 用WP_Query的话,一定要记得在循环结束后调用wp_reset_postdata(),这样能避免后续的模板标签(比如the_post())出错。

内容的提问来源于stack exchange,提问作者Onur Aydeniz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:33:04