1

I'm trying to use the <?php rewind_posts(); ?> feature to use two loops on my homepage template. Does anyone know how to stop the first loop after the latest post?

Here is my Code:

<?php 

            if (have_posts()) : while (have_posts()) : the_post(); ?>
            <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>

            <?php endwhile; endif; ?>

            <?php rewind_posts(); ?>

            <?php
                if ( have_posts() ) :

                    if ( is_home() && ! is_front_page() ) :
                        ?>
                            <header>
                                <h1 class="page-title screen-reader-text">
                                    <?php single_post_title(); ?>
                                </h1>
                            </header>
                            <?php
                    endif;

                    while ( have_posts() ) :
                        the_post();

                        get_template_part( 'template-parts/content-home', get_post_type() );

                    endwhile;

                    the_posts_navigation();

                else :

                    get_template_part( 'template-parts/content', 'none' );

                endif;
            ?>

3 Answers 3

0

Try like this which fill skip first post:

$i = 0;
while ( have_posts() ) :
    the_post();
    if($i > 0)
        get_template_part( 'template-parts/content-home', get_post_type() );
    else {
        // design your first post as you wish with HTML, CSS
        the_title();
        the_permalink();
        the_post_thumbnail();   
        the_content();
        the_author();
        the_date();
    }           
    $i++;
endwhile;
1

You can output the first post outside of a loop, then loop over the remaining posts:

if( have_posts() ){

    // first post output
    the_post();
    the_title();

    while( have_posts() ){
        // the rest
        the_post();
        the_title();
    }
}
1

I like more the option and syntax of current_post:

// First post inside loop
if( $wp_query->current_post === 0 ) { 
            
}

See the WordPress Codex for more details: Object orientation of the Loop

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.