1

On the main blog page for a site I am developing, my designer has the first post styled uniquely and in a different section on the page than the rest. Since it is a paginated archive page, I need to loop through all posts on the paginated page just excluding the first post, as I will get that in it's own loop.

I tried the Offset option within the query, but learned that that kills pagination which won't work.

Thanks for the help.

2

2 Answers 2

3

You don't need separate queries, you can run multiple loops on the same query-

// output first post
if( have_posts() ){
    the_post();
    the_title();
}

// output the rest of the posts...
if( have_posts() ){
    while( have_posts() ){
        the_post();
        the_title();
    }
}

You can also use rewind_posts() to reset the current post back to 0, as well as manually set $wp_query->current_post to whatever index you want and start the loop there (note: the post counter starts at 0, not 1).

If you only want to style the first post on the first page and not subsequent pages, you can check if it's not paged with ! is_paged()

if( ! is_paged() ){
    echo 'this will only output on the first page';
    if( have_posts() ){
        the_post();
        the_title();
    }
}
1
  • the above suggestion is not working.
    – golchha21
    Commented May 2, 2020 at 14:46
0

Following this step:

Step 1: For different section you can display first Posts. when put this code.

<?php
// For your frist post
global $post,$firstPosts;
$args = array(
            'posts_per_page' => 1,
            'orderby' => 'post_date',
            'order' => 'DESC',
            'post_type' => 'post',
            'post_status' => 'publish',
        );
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
    while ( $the_query->have_posts() ) : $the_query->the_post();
        $firstPosts[] = $post->ID;
        echo get_the_title();  
    endwhile;
endif;
wp_reset_postdata();
?>

Step 2: On your blog page listing. Put this code in Index.php file. for Loop posts only excluding first post

<?php
// blog page
// Put this code after get_header(); line

global $wp_query,$firstPosts;
$args = array_merge( $wp_query->query,  array( 'post__not_in' => $firstPosts ) );
query_posts( $args ); 

?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.