users - Can I use paginate_links() to add a pagination list, yet prevent it from inserting links?

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

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
    }

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.

  • http://example/sample-page/page/2/
  • http://example/sample-page/page/3/
  • http://example/sample-page/page/4/

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
    }
Share Improve this question asked Jul 26, 2019 at 12:23 cag8fcag8f 1,9973 gold badges21 silver badges31 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 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/reference/hooks/paginate_links/

Hope that helps!!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745292871a4620966.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信