6

I am using the following code for my pagination.

// Pagination
function my_paginate_links() {
    global $wp_rewrite, $wp_query;
    $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
    $pagination = array(
        'base' => @add_query_arg('page','%#%'),
        'format' => '',
        'total' => $wp_query->max_num_pages,
        'current' => $current,
        'prev_text' => __(''),
        'next_text' => __('next »'),
        'end_size' => 1,
        'mid_size' => 2,
        'show_all' => true,
        'type' => 'list'
    );
    if ( $wp_rewrite->using_permalinks() )
            $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' );
    if ( !empty( $wp_query->query_vars['s'] ) )
            $pagination['add_args'] = array( 's' => get_query_var( 's' ) );
    echo paginate_links( $pagination );
}

When it's on the first page, the 'previous' link doesn't show. I understand why but I'd like it to show because I am designing the site a certain way. Is there a part of the code above I can change to make this happen? I have read through the codex regarding this but couldn't find an answer that made sense to me. I'd appreciate any guidance. Thank you.

3 Answers 3

4

Looking at the source of paginate_links() it seems there is no option available to always include a previous or next link. The function simply compares the current page number with the total page number to determine whether these links need to be added.

Working around this problem is possible, though. This should get you started:

$page_links = paginate_links(array(
    // Make the function never return previous or next links,
    // we will add them manually.
    'prev_next' => FALSE,

    // Give us back an array, this is the easiest to work with.
    'type' => 'array',

    // + all your other arguments here
));

// Now it is just a matter of adding a previous and next link manually.
// Note: you still have to build the actual prev/next URLs.
$prev_link = '<a href="#">'.__('Previous').'</a>';
$next_link = '<a href="#">'.__('Next').'</a>';

array_unshift($page_links, $prev_link);
$page_links[] = $next_link;

// Output
echo implode($page_links);
1

A little bit late, but I just had this problem and found your unsolved issue. Here's what I came to... We can get an array of link, so, if we compare the page number we are on, we can manually add our prev/next link and loop the rest of the links:

    // Pagination
function my_paginate_links() {
    global $wp_rewrite, $wp_query;
    $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
    $pagination = array(
        'base' => @add_query_arg('page','%#%'),
        'format' => '',
        'total' => $wp_query->max_num_pages,
        'current' => $current,
        'prev_text' => __(''),
        'next_text' => __('next &raquo;'),
        'end_size' => 1,
        'mid_size' => 2,
        'show_all' => true,
        'type' => 'array'
    );
    if ( $wp_rewrite->using_permalinks() )
            $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' );
    if ( !empty( $wp_query->query_vars['s'] ) )
            $pagination['add_args'] = array( 's' => get_query_var( 's' ) );
    $pages = paginate_links( $pagination );
    echo '<ul>';
    if ( $paged == 1) echo '<li><a href="#" class="disabled">&laquo;</a></li>';
    foreach ($pages as $page) :
        echo '<li>'.$page.'</li>';
    endforeach;
    if ( $paged == $wp_query->max_num_pages ) echo '<li><a href="#" class="disabled">&raquo;</a></li>';
    echo '</ul>';
}
0

We also encountered this requirement. We found we wanted the placeholder element to be a <span> and not an <a> for accessibility reasons, and to match the behavior of the current page number, which is also non-clickable.

Tried some of the solutions on this page, but it ended up just being easier to check if the previous or next link exists in the output, and add the placeholder if not.

Here's my solution:

$page_links = paginate_links();
if ( ! str_contains( $page_links, 'class="prev' ) ) { // phpcs:ignore PHPCompatibility.FunctionUse.NewFunctions.str_containsFound
    $page_links = '<span class="prev page-numbers placeholder-prevnext">Previous</span>' . $page_links;
}
if ( ! str_contains( $page_links, 'class="next' ) ) { // phpcs:ignore PHPCompatibility.FunctionUse.NewFunctions.str_containsFound
    $page_links .= '<span class="next page-numbers placeholder-prevnext">Next</span>';
}

echo $page_links;

and if you need <PHP8 support, add

// based on original work from the PHP Laravel framework
if ( ! function_exists( 'str_contains' ) ) {
    function str_contains( $haystack, $needle ) {
        return $needle !== '' && mb_strpos( $haystack, $needle ) !== false; //phpcs:ignore WordPress.PHP.YodaConditions.NotYoda
    }
}

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.