0

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.

2 Answers 2

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.

0

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');
1
  • Thanks for the answer but I added to functions.php and nothing changes. Commented Mar 11, 2019 at 16:04

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.