How to skip the first post in wordpress loop?

by jasen_gottlieb , in category: PHP CMS , 5 months ago

How to skip the first post in wordpress loop?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by dana , 5 months ago

@jasen_gottlieb 

To skip the first post in the WordPress loop, you can use the "offset" parameter in the loop query. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<?php
$args = array(
    'offset' => 1,  // Skip the first post
);

$query = new WP_Query($args);

if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();
        // Display your post content here
    }
}

wp_reset_postdata();
?>


In this example, the $args array is passed to the WP_Query class, and the offset parameter is set to 1, which indicates to skip the first post in the loop.


Then, the loop iterates through the remaining posts using the have_posts() and the_post() functions, allowing you to display the post content as desired.


Finally, don't forget to reset the post data after the loop using wp_reset_postdata().