1

My page displays a list of WordPress users. For pagination, I have used the function paginate_links. This inserts a list onto my page e.g.

  • page 2
  • page 3
  • page 4

(screenshot). And each list item is a link to a new page which displays the paginated results, e.g.

My code is below.

Is there a way to use paginate_links() to just insert the list, yet refrain from inserting links into each item, and refrain from creating new pages?

The motivation for this is that I want the list to display, but I want to add my own link to each list item. Each link will be one that makes an AJAX call to my server, fetches new data, and displays it all on the same page. When a visitor clicks one of these links, I do not want him to be redirected to a different page, and I do not want a new page to be created--both seem to be default behavior for paginate_links().

My existing code

<?php 


    $number      = 10; // Update this according to your needs. Users perpage  
    $paged       = (get_query_var('paged')) ? get_query_var('paged') : 1;  
    $offset      = ($paged - 1) * $number;  
    $users       = get_users();  

    $args = array(
        'orderby'      => 'login',
        'order'        => 'ASC',
        'offset'       => $offset,
        'number'       => $number,
        'count_total'  => false,
    ); 

    $query       = get_users($args);  

    $total_users = count($users);  
    $total_query = count($query);  
    $total_pages = intval($total_users / $number) + 1; 

    echo $total_users . ' ' . $total_query . ' ' . $total_pages;

    if ($total_users > $total_query) {
        ?>
        <div id="pagination" class="clearfix">  
            <span class="pages">Pages:</span>  
            <?php
            $current_page = max(1, get_query_var('paged'));  
            echo paginate_links(array(  
                'base'      => get_pagenum_link(1) . '%_%',  
                'format'    => 'page/%#%/',  
                'current'   => $current_page,  
                'total'     => $total_pages,  
                'prev_next' => false,  
                'type'      => 'list',  
            ));  
            ?>
        </div>  
        <?php
    }
0

1 Answer 1

2

There are three types of outputs you can get from paginate_links(), but all of them are going to have <a> inside of them.

I think an easy solution would be to use PHP to just strip out the <a> tag for you. Rather than echoing back paginate_links(), store that in a variable and then use preg_replace() to take out the anchors and echo that back:

$pagination_link_output = paginate_links(array(  
    'base'      => get_pagenum_link(1) . '%_%',  
    'format'    => 'page/%#%/',  
    'current'   => $current_page,  
    'total'     => $total_pages,  
    'prev_next' => false,  
    'type'      => 'list',  
));  

echo preg_replace('#<a.*?>([^>]*)</a>#i', '$1', $pagination_link_output);

You can also filter if you wanted to go that route: https://developer.wordpress.org/reference/hooks/paginate_links/

Hope that helps!!

1
  • 1
    I see your suggestion, thanks. In light of that, I opted for an option in which I didn't use paginate_links() at all--I simply built my list and links manually with PHP. So how should we handle this question, and your reply? It sounds like you answered my original question with 'no,' and provided an alternate solution/workaround which I was never able to test. So I'll just mark yours as the answer--I don't have a problem with that.
    – cag8f
    Commented Aug 3, 2019 at 11:13

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.