Pagination: How do I always show ‘previous’?

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.

Related posts

Leave a Reply

2 comments

  1. 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);
    
  2. 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>';
    }