如何显示WP_USER_QUERY的分页链接?

时间:2012-07-04 作者:Osu

我想我已经差不多做到了,但是我无法获得分页链接来显示我正在创建的作者目录。

下面是我的代码,但我不知道如何获得链接,以便在作者页面之间导航。有人能帮我吗?我觉得这可能有用,但我不知道如何实现它:

paginate_links()

谢谢

Osu

    <?php 
/* ****************************************************************** */
                        /* !LIST AUTHORS */
/* ****************************************************************** */ 

// THANKS TO:
// http://www.mattvarone.com/wordpress/list-users-with-wp_user_query/

// pagination
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1; // Needed for pagination
$paged -= 1;
$limit = 2;
$offset = $paged * $limit;

// prepare arguments
$args  = array(
    // search only for Authors role
    \'role\'      => \'Subscriber\',
    // order results by display_name
    \'orderby\'   => \'display_name\',
    // return all fields
    \'fields\'    => \'all_with_meta\',
    \'number\'    => $limit,
    \'offset\'    => $offset      
);
// Create the WP_User_Query object
$wp_user_query = new WP_User_Query($args);
// Get the results
$authors = $wp_user_query->get_results();
// Check for results
if (!empty($authors))
{
    echo \'<div class="author-entry">\';
    // loop trough each author
    foreach ($authors as $author)
    {
        $author_info = get_userdata($author->ID); ?>

        <span style="float:left;padding:0 5px 0 0;"><?php echo get_avatar( $author->ID, 50 ); /* http://codex.wordpress.org/Function_Reference/get_avatar */ ?></span>
        <span class="fn"><strong>First name</strong> : <?php echo $author_info->first_name; ?></span><br />
        <span class="ln"><strong>Last name</strong> : <?php echo $author_info->last_name; ?></span><br />
        <span class="em"><strong>Email address</strong> : <a href="mailto:<?php echo $author_info->user_email; ?>"><?php echo $author_info->user_email; ?></a></span><br />
        <span class="we"><strong>Website</strong> : <a href="<?php echo $author_info->user_url; ?>"><?php echo $author_info->user_url; ?></a></span><br />

        <span class="de"><strong>Bio</strong> :<br /><?php echo $author_info->description ; ?></span>
        <div class="clear">&nbsp;</div>

    <?php 
    }
    echo \'</div>\';
} else {
    echo \'No authors found\';
}
?>

<?php /* WHAT DO I PUT HERE TO CREATE THE PAGINATION LINKS? */ ?>

3 个回复
最合适的回答,由SO网友:Pippin 整理而成

这会让你离得很近。我还没有测试过它,但它与我使用过几次的设置几乎相同。

/*
 * We start by doing a query to retrieve all users
 * We need a total user count so that we can calculate how many pages there are
 */

$count_args  = array(
    \'role\'      => \'Subscriber\',
    \'fields\'    => \'all_with_meta\',
    \'number\'    => 999999      
);
$user_count_query = new WP_User_Query($count_args);
$user_count = $user_count_query->get_results();

// count the number of users found in the query
$total_users = $user_count ? count($user_count) : 1;

// grab the current page number and set to 1 if no page number is set
$page = isset($_GET[\'p\']) ? $_GET[\'p\'] : 1;

// how many users to show per page
$users_per_page = 5;

// calculate the total number of pages.
$total_pages = 1;
$offset = $users_per_page * ($page - 1);
$total_pages = ceil($total_users / $users_per_page);


// main user query
$args  = array(
    // search only for Authors role
    \'role\'      => \'Subscriber\',
    // order results by display_name
    \'orderby\'   => \'display_name\',
    // return all fields
    \'fields\'    => \'all_with_meta\',
    \'number\'    => $users_per_page,
    \'offset\'    => $offset // skip the number of users that we have per page  
);

// Create the WP_User_Query object
$wp_user_query = new WP_User_Query($args);

// Get the results
$authors = $wp_user_query->get_results();

// check to see if we have users
if (!empty($authors))
{
    echo \'<div class="author-entry">\';
    // loop trough each author
    foreach ($authors as $author)
    {
        $author_info = get_userdata($author->ID); ?>

        <span style="float:left;padding:0 5px 0 0;"><?php echo get_avatar( $author->ID, 50 ); /* http://codex.wordpress.org/Function_Reference/get_avatar */ ?></span>
        <span class="fn"><strong>First name</strong> : <?php echo $author_info->first_name; ?></span><br />
        <span class="ln"><strong>Last name</strong> : <?php echo $author_info->last_name; ?></span><br />
        <span class="em"><strong>Email address</strong> : <a href="mailto:<?php echo $author_info->user_email; ?>"><?php echo $author_info->user_email; ?></a></span><br />
        <span class="we"><strong>Website</strong> : <a href="<?php echo $author_info->user_url; ?>"><?php echo $author_info->user_url; ?></a></span><br />

        <span class="de"><strong>Bio</strong> :<br /><?php echo $author_info->description ; ?></span>
        <div class="clear">&nbsp;</div>

    <?php 
    }
    echo \'</div>\';
} else {
    echo \'No authors found\';
}

// grab the current query parameters
$query_string = $_SERVER[\'QUERY_STRING\'];

// The $base variable stores the complete URL to our page, including the current page arg

// if in the admin, your base should be the admin URL + your page
$base = admin_url(\'your-page-path\') . \'?\' . remove_query_arg(\'p\', $query_string) . \'%_%\';

// if on the front end, your base is the current page
//$base = get_permalink( get_the_ID() ) . \'?\' . remove_query_arg(\'p\', $query_string) . \'%_%\';

echo paginate_links( array(
    \'base\' => $base, // the base URL, including query arg
    \'format\' => \'&p=%#%\', // this defines the query parameter that will be used, in this case "p"
    \'prev_text\' => __(\'&laquo; Previous\'), // text for previous page
    \'next_text\' => __(\'Next &raquo;\'), // text for next page
    \'total\' => $total_pages, // the total number of pages we have
    \'current\' => $page, // the current page
    \'end_size\' => 1,
    \'mid_size\' => 5,
));

SO网友:Radley Sustaire

你真的不应该用皮平的答案。查询效率很低。$user_count_query 在本例中,可以将数据库中最多999999个用户以及所有用户字段返回到脚本中。如果/当您的站点足够大时,这肯定会达到PHP的内存和/或时间限制。

但这可能是2012年唯一的解决方案。

这里有一个更好的方法。在这个例子中,我只有next and previous page 但是如果您确实需要编号分页,那么可以使用变量来构建它。据我所知,WordPress没有与WP\\u User\\u Query兼容的分页功能。

<?php

// Pagination vars
$current_page = get_query_var(\'paged\') ? (int) get_query_var(\'paged\') : 1;
$users_per_page = 2; // RAISE THIS AFTER TESTING ;)

$args = array(
    \'number\' => $users_per_page, // How many per page
    \'paged\' => $current_page // What page to get, starting from 1.
);

$users = new WP_User_Query( $args );

$total_users = $users->get_total(); // How many users we have in total (beyond the current page)
$num_pages = ceil($total_users / $users_per_page); // How many pages of users we will need

?>
    <h3>Page <?php echo $current_page; ?> of <?php echo $num_pages; ?></h3>
    <p>Displaying <?php echo $users_per_page; ?> of <?php echo $total_users; ?> users</p>

    <table>
        <thead>
            <tr>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Email</th>
            </tr>
        </thead>

        <tbody>
            <?php
            if ( $users->get_results() ) foreach( $users->get_results() as $user )  {
                $firstname = $user->first_name;
                $lastname = $user->last_name;
                $email = $user->user_email;
                ?>
                <tr>
                    <td><?php echo esc_html($firstname); ?></td>
                    <td><?php echo esc_html($lastname); ?></td>
                    <td><?php echo esc_html($email); ?></td>
                </tr>
                <?php
            }
            ?>
        </tbody>
    </table>

    <p>
        <?php
        // Previous page
        if ( $current_page > 1 ) {
            echo \'<a href="\'. add_query_arg(array(\'paged\' => $current_page-1)) .\'">Previous Page</a>\';
        }

        // Next page
        if ( $current_page < $num_pages ) {
            echo \'<a href="\'. add_query_arg(array(\'paged\' => $current_page+1)) .\'">Next Page</a>\';
        }
        ?>
    </p>
第2页示例:

table of users, starting at page 2

<2018年6月8日更新:如果您想page numbers 这里不是下一页/上一页链接,而是如何设置。请注意,您需要将数字替换为页面链接,在本例中它们将不可单击(基于https://stackoverflow.com/a/11274294/470480, 修改为显示一致数量的中间数字,而不是添加“…”除非实际跳过了页面)。

您还可以看到my gist file 其中包含用于此目的的可重用函数。

$current_page = 5; // Example
$num_pages = 10; // Example

$edge_number_count = 2; // Change this, optional

$start_number = $current_page - $edge_number_count;
$end_number = $current_page + $edge_number_count;

// Minus one so that we don\'t split the start number unnecessarily, eg: "1 ... 2 3" should start as "1 2 3"
if ( ($start_number - 1) < 1 ) {
    $start_number = 1;
    $end_number = min($num_pages, $start_number + ($edge_number_count*2));
}

// Add one so that we don\'t split the end number unnecessarily, eg: "8 9 ... 10" should stay as "8 9 10"
if ( ($end_number + 1) > $num_pages ) {
    $end_number = $num_pages;
    $start_number = max(1, $num_pages - ($edge_number_count*2));
}

if ($start_number > 1) echo " 1 ... ";

for($i=$start_number; $i<=$end_number; $i++) {
    if ( $i === $current_page ) echo " [{$i}] ";
    else echo " {$i} ";
}

if ($end_number < $num_pages) echo " ... {$num_pages} ";
输出(从第1页到第10页):

[1]  2  3  4  5  ... 10 
1  [2]  3  4  5  ... 10 
1  2  [3]  4  5  ... 10 
1  2  3  [4]  5  ... 10 

1 ...  3  4  [5]  6  7  ... 10 
1 ...  4  5  [6]  7  8  ... 10 

1 ...  6  [7]  8  9  10
1 ...  6  7  [8]  9  10
1 ...  6  7  8  [9]  10
1 ...  6  7  8  9  [10]

SO网友:The Sumo

radley sustaire的回答应该得到充分肯定,但我发现了一个小问题,所以我在这里分享我的答案版本。

在我的版本中,我还按位置、关键字等过滤结果,因此某些页面的结果少于“$users\\u per\\u page”变量。因此,例如,如果我的每页用户数设置为显示10,但过滤器的结果仅返回3个用户,我会在页面顶部显示“显示3个用户中的10个”。显然这没有意义,所以我添加了一个简单的“if”语句来检查结果计数是否高于“$users\\u per\\u page”变量。

Radley,如果你用最新的答案编辑你的答案,我会很高兴地投票支持它,因为我认为它比Pippin的解决方案好。

因此,这是任何想要它的人的最终代码。

<?php

// Pagination vars
$current_page = get_query_var(\'paged\') ? (int) get_query_var(\'paged\') : 1;
$users_per_page = 10;

$args = array(
    \'number\' => $users_per_page, // How many per page
    \'paged\' => $current_page // What page to get, starting from 1.
);

$users = new WP_User_Query( $args );

$total_users = $users->get_total(); // How many users we have in total (beyond the current page)
$num_pages = ceil($total_users / $users_per_page); // How many pages of users we will need

if ($total_users < $users_per_page) {$users_per_page = $total_users;}

?>
    <h3>Page <?php echo $current_page; ?> of <?php echo $num_pages; ?></h3>
    <p>Displaying <?php echo $users_per_page; ?> of <?php echo $total_users; ?> users</p>

    <table>
        <thead>
            <tr>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Email</th>
            </tr>
        </thead>

        <tbody>
            <?php
            if ( $users->get_results() ) foreach( $users->get_results() as $user )  {
                $firstname = $user->first_name;
                $lastname = $user->last_name;
                $email = $user->user_email;
                ?>
                <tr>
                    <td><?php echo esc_html($firstname); ?></td>
                    <td><?php echo esc_html($lastname); ?></td>
                    <td><?php echo esc_html($email); ?></td>
                </tr>
                <?php
            }
            ?>
        </tbody>
    </table>

    <p>
        <?php
        // Previous page
        if ( $current_page > 1 ) {
            echo \'<a href="\'. add_query_arg(array(\'paged\' => $current_page-1)) .\'">Previous Page</a>\';
        }

        // Next page
        if ( $current_page < $num_pages ) {
            echo \'<a href="\'. add_query_arg(array(\'paged\' => $current_page+1)) .\'">Next Page</a>\';
        }
        ?>
    </p>

结束

相关推荐

How to use pagination?

我有一个页面的以下代码,在这里我拉一张照片,标题和最后10篇文章的摘录。问题是,我不知道如何添加分页?我也安装了WP PageNavi。我已经看到一些类似的问题得到了解决,但我根本无法真正理解这个概念,我首先尝试确保这是可能的?<?php /* Template Name: Opinion */ ?> <?php get_header(); ?> <div id=\"primary\"> <div id=\"