【问题标题】:How to paginate using wp_user_query如何使用 wp_user_query 进行分页
【发布时间】:2018-03-04 18:50:18
【问题描述】:

我一直很难使用自定义插件在我的 wordpress 网站后端为用户列表进行分页。它实际上显示了带有链接的所有内容,但是当我单击“下一页”链接时,它会加载并保持不变,而不是移动到第 2 页。这是我的代码(我从 stackoverflow 上的另一个答案中得到这个,但没有成功使分页链接增加到下一页):

   function oragatesoft_expert(){

// Pagination vars
$current_page = get_query_var('paged') ? (int) get_query_var('paged') : 1;
echo $current_page;
$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>
  <?php
}

【问题讨论】:

    标签: php wordpress pagination


    【解决方案1】:

    我可以确认,使用这样的东西对遇到这种情况的任何人都有效。

    $args = array (
        'number' => 1, // use this like you would posts_per_page
        'paged' => 2,
    );
    
    $wp_user_query = new \WP_User_Query($args);
    
    // Get the results
    $users = $wp_user_query->get_results();
    

    【讨论】:

      猜你喜欢
      • 2019-02-03
      • 2012-11-10
      • 1970-01-01
      • 2011-06-17
      • 2013-06-16
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      • 1970-01-01
      相关资源
      最近更新 更多