Search code examples
wordpresscustom-wordpress-pages

Do not display the last post on the archive page


I need the last post not to be displayed on the post archive page.

What should I do to do this?

You can see the page code of my page archive below.

<?php
    /* Start the Loop */
    $i=0;
    while ( have_posts() ) :
        if($i==5) {
            echo '
                adv code
            ';
        }
        the_post();

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

    $i++;
    endwhile;

    custom_pagination();

    else :

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

    endif;
?>

note: Only the last post. Because the last post of the page archive is displayed manually in the slider of that page. I do not want the last post in the list to be displayed and repeated.


Solution

  • Using offset to the query will do the trick like

    function exclude_last_post( $query ) {
        if ( $query->is_main_query() && !is_admin() && is_archive() ) {
            $query->set( 'offset', '1' );
        }
    }
    add_action( 'pre_get_posts', 'exclude_last_post' );