$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Skip latest 3 posts from loop|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Skip latest 3 posts from loop

matteradmin11PV0评论

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');

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far