How to output different posts per page?

I want to display one blog article less than on the following pages. The following code works but the the paginaton on the first page displays 1, 2, 3, 4 and on the next pages i get the correct pagination of 1, 2, 3 – What am i doing wrong?

add_action( 'pre_get_posts', 'trr_query_offset', 1 );
function trr_query_offset( &$query ) {
    if ( ! ( $query->is_home() || is_main_query() ) ) {
        return;
    }

    $offset = -1;

    $ppp = get_option( 'posts_per_page' );

    if ( $query->is_paged ) {
        $page_offset = $offset + ( ( $query->query_vars['paged']-1 ) * $ppp );
        $query->set( 'offset', $page_offset );
    } else {
        $query->set( 'posts_per_page', $offset + $ppp );
    }
}

add_filter( 'found_posts', 'trr_adjust_offset_pagination', 1, 2 );
function trr_adjust_offset_pagination( $found_posts, $query ) {
    $offset = -1;

    if ( $query->is_home() && is_main_query() ) {
        return $found_posts - $offset;
    }
    return $found_posts;
}

Update

I’m using a custom Theme based on Chisel with Timber. This is my home.twig:

[...]
<div class="tool-pagination">
    {% if posts.pagination.prev %}
      <a href="https://wordpress.stackexchange.com/questions/362809/{{posts.pagination.prev.link}}" class="prev {{posts.pagination.prev.link|length ? '' : 'invisible'}}">Prev</a>
    {% endif %}
    <ul class="pages">
      {% for page in posts.pagination.pages %}
        <li>
          {% if page.link %}
            <a href="{{page.link}}" class="{{page.class}}">{{page.title}}</a>
          {% else %}
            <span class="{{page.class}}">{{page.title}}</span>
          {% endif %}
        </li>
      {% endfor %}
    </ul>
    {% if posts.pagination.next %}
      <a href="{{posts.pagination.next.link}}" class="next {{posts.pagination.next.link|length ? '' : 'invisible'}}">Next</a>
    {% endif %}
  </div>

Solution

I stumbled upon a similar question here on stackexchange which helped with my problem: Take a look at Case #2 and especially the comment of mach

0

Leave a Comment