복붙노트

[WORDPRESS] WordPress Loop에서 첫 페이지에 다른 수의 게시물

WORDPRESS

WordPress Loop에서 첫 페이지에 다른 수의 게시물

해결법


  1. 1.이것은 나를 위해 일했습니다!

    이것은 나를 위해 일했습니다!

    function tax_and_offset_homepage( $query ) {
    if ($query->is_home() && $query->is_main_query() && !is_admin()) {
    $query->set( 'post_type', 'my_post_type' );
    $query->set( 'post_status', 'publish' );
    $query->set( 'ignore_sticky_posts', '-1' );
    $tax_query = array(
        array(
            'taxonomy' => 'my_taxo',
            'field' => 'slug',
            'terms' => array('slug1', 'slug2', 'slug3')
        )
    );
    $query->set( 'tax_query', $tax_query );
    $ppp = get_option('posts_per_page');
    $offset = 1;
    if (!$query->is_paged()) {
      $query->set('posts_per_page',$offset + $ppp);
    } else {
      $offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
      $query->set('posts_per_page',$ppp);
      $query->set('offset',$offset);
    }
    }
    }
    add_action('pre_get_posts','tax_and_offset_homepage');
    
    function homepage_offset_pagination( $found_posts, $query ) {
    $offset = 1;
    
    if( $query->is_home() && $query->is_main_query() ) {
        $found_posts = $found_posts - $offset;
    }
    return $found_posts;
    }
    add_filter( 'found_posts', 'homepage_offset_pagination', 10, 2 );
    
  2. from https://stackoverflow.com/questions/28256888/different-number-of-posts-on-first-page-in-wordpress-loop by cc-by-sa and MIT license