Skip to content
Advertisement

Add anchor to page 1 prev link from page 2 when paginated

I have used the following for pagination:

<?php endwhile;
     echo "<div class='paginate_wrap'>";
     echo paginate_links ( array (
          'add_fragment' => '#project_area',
          'format' => '?paged=%#%',
          'current' => max( 1, get_query_var('paged') ),
           'total' => $related->max_num_pages,
           'prev_text' => '',
           'next_text' => ''
     ) );
     echo "</div>";
     endif; ?>

#project-area is used to scroll the user down to the desired section of the page.

This works accept for when the user is on page 2, and hits the back arrow to go to page 1. This currently hyperlinks to the base URL without the desired #project_area appended.

What would I need to change to add #project_area to the page 1 url only from the prev hyperlink on page 2, without ?paged=1 applied?

Advertisement

Answer

Try with ‘base’ argument

    $big = 999999999;
    echo paginate_links ( array (
          'base' => str_replace( $big, '%#%', get_pagenum_link( $big, false ) ),
          'add_fragment' => '#project_area',
          'format' => '?paged=%#%',
          'current' => max( 1, get_query_var('paged') ),
          'total' => $related->max_num_pages,
          'prev_text' => '',
          'next_text' => ''
     ) );
     echo "</div>";
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement