1

Is there a way to add additional first and last page links to the pagination object if it is rendere with the the_posts_pagination() method?

At this point I have the following

the_posts_pagination(
              [
                'screen_reader_text' => ' ',
                'mid_size'  => 2,
                'prev_text' => __( 'vorherige', 'bdb' ),
                'next_text' => __( 'nächste', 'bdb' ),
              ]);

and I would like to after prev_text a first page link if is not the first page and a last page link before next_text if is not the last page

2 Answers 2

0

the_posts_pagination() is an echo wrapper for get_the_posts_pagination(). The getter generates pagination links with paginate_links(), which returns a string with the links separated by a newline character by default.

This string is passed to _navigation_markup() to get the complete pagination markup. We can manipulate the paginate_links() output with paginate_links_output filter, before the navigation markup is generated.

The filter recieves the default links string and pagination arguments as parameters. From $args you can get the total page count as well as the link format.

Here's an example implementation where the filtering is limited to the specific use case by first adding the filter and then removing it after calling the pagination function. The custom template function would be used insted of the_posts_pagination() in template files.

function my_pagination_link_url(array $args, int $current, string $text) {
    // duplicated / modified from https://developer.wordpress.org/reference/functions/paginate_links/
    $link = str_replace( '%_%', $args['format'], $args['base'] );
    $link = str_replace( '%#%', $current, $link );

    return sprintf(
        '<a class="page-numbers" href="%s">%s</a>',
        $link,
        $text
    );
}

function my_first_pagination_link(array $args) {
  return my_pagination_link_url(
    $args, 1, 'My first'
  );
}

function my_last_pagination_link(array $args) {
  return my_pagination_link_url(
    $args, $args['total'], 'My last'
  );
}

// $args, https://developer.wordpress.org/reference/functions/paginate_links/
function my_add_links_to_pagination( $pagination_html, $args ) {
    return implode('', [
        my_first_pagination_link($args),
        $pagination_html,
        my_last_pagination_link($args),
        // ...my everything?
    ]);
}

function my_the_posts_pagination() {
  add_filter( 'paginate_links_output', 'my_add_links_to_pagination', 10, 2 );

  the_posts_pagination([
    'screen_reader_text' => ' ',
    'mid_size'  => 2,
    'prev_text' => __( 'vorherige', 'bdb' ),
    'next_text' => __( 'nächste', 'bdb' ),
  ]);

  remove_filter( 'paginate_links_output', 'my_add_links_to_pagination', 10 );
}

P.S. Modify the filters and add logic as needed.

1
  • thanks for hints. I got it working!
    – fefe
    Commented May 18, 2022 at 13:43
0

paginate_links() is probably what you want after playing a little with the params

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.