Skip latest 3 posts from loop

admin2025-06-02  2

I downloaded a starter theme (Underscores). I want to exclude first 3 post from my loop on index page.

Here is the loop;

<?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;
    /* Start the Loop */
    while ( have_posts() ) :
        the_post();
        get_template_part( 'template-parts/content', get_post_type() );
    endwhile;
    the_posts_navigation();
else :
    get_template_part( 'template-parts/content', 'none' );
endif;
?>

Thanks.

I downloaded a starter theme (Underscores). I want to exclude first 3 post from my loop on index page.

Here is the loop;

<?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;
    /* Start the Loop */
    while ( have_posts() ) :
        the_post();
        get_template_part( 'template-parts/content', get_post_type() );
    endwhile;
    the_posts_navigation();
else :
    get_template_part( 'template-parts/content', 'none' );
endif;
?>

Thanks.

Share Improve this question asked Mar 10, 2019 at 11:11 Kerem BeyazitKerem Beyazit 1
Add a comment  | 

2 Answers 2

Reset to default 0

You can use the pre_get_posts action to alter your main query. The codex has a note that using the action might break pagination.

Have a look at this old question and answer, Changing Posts Per Page and offset with pre_get_posts It has code examples for using the pre_get_posts and how to handle the pagination problem too.

This is code will look like that should go into functions.php file

function exclude_first_3_posts($query){
  if(!is_admin() && $query->is_main_query() && is_home() && !is_front_page()){
    $query->set('post_type','post');
    $query->set('offset',3);
    $query->set('order','desc');
  }
}
add_action('pre_get_posts','exclude_first_3_posts');
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748821122a314003.html

最新回复(0)