2

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="{{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

2
  • Are you using default theme? What is your pagination display template and logic? How's it design? It sounds like your template for pagination logic might have some flaw if the pagination is customised by it I guess. Mar 31, 2020 at 0:36
  • @simongcc thanks for pointing that out, i have updated the Post!
    – moeses
    Mar 31, 2020 at 10:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.