如何在Wordpress中对用户结果页面进行分页

时间:2014-04-24 06:32:37

标签: wordpress pagination

这是我的代码,任何人都可以帮助我如何在用户结果页面中添加分页?

if (!empty($users)){    

foreach($users as $user){
    $user = get_userdata($user->ID);


    <div class="wg_rec_dashboard_img">
        <?php $wg_front_dp = get_user_meta($user->ID, 'wg_dp', true);?>
        <img src="<?php echo $wg_front_dp; ?>">
    </div>

    <div class="wg_rec_username">
         <?php echo $user->first_name; ?>
    </div>

我试过Wordpress&#39;内置分页 ,但它不在这里工作。

2 个答案:

答案 0 :(得分:0)

您可以使用以下内容;

<?php   
    $number      = 50; // 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();  
    $query       = get_users('&offset='.$offset.'&number='.$number);  
    $total_users = count($users);  
    $total_query = count($query);  
    $total_pages = intval($total_users / $number) + 1;  

    // Iterate perpage users
    foreach($query as $q){
    $user = get_userdata($q->ID);
    ?>

    <div class="wg_rec_dashboard_img">
        <?php $wg_front_dp = get_user_meta($user->ID, 'wg_dp', true);?>
        <img src="<?php echo $wg_front_dp; ?>">
    </div>

    <div class="wg_rec_username">
         <?php echo $user->first_name; ?>
    </div>
    <?php } ?>

    // Pagination part
    <?php
    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>  

答案 1 :(得分:0)

将此代码放在主题function.php中(您可以根据需要使用CSS设置页面导航的样式):

PHP:

if (!function_exists('_numeric_posts_nav')) {
    function _numeric_posts_nav() {
    global $wp_query;
    /** Stop execution if there's if it is single past/page or only 1 page */
        if( is_singular() ) return;
        if( $wp_query->max_num_pages <= 1 ) return;

        $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
        $max   = intval( $wp_query->max_num_pages );

        /** Add current page to the array */
        if ( $paged >= 1 )
            $links[] = $paged;

        /** Add the pages around the current page to the array */
        if ( $paged >= 3 ) {
            $links[] = $paged - 1;
            $links[] = $paged - 2;
        }

        if ( ( $paged + 2 ) <= $max ) {
            $links[] = $paged + 2;
            $links[] = $paged + 1;
        }

        echo '<div class="navigation"><ul>' . "\n";
        /** Previous Post Link */
        if ( get_previous_posts_link() )
            printf( '<li>%s</li>' . "\n", get_previous_posts_link() );
        /** Link to first page, plus ellipses if necessary */
        if ( ! in_array( 1, $links ) ) {
            $class = 1 == $paged ? ' class="active"' : '';
            printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
            if ( ! in_array( 2, $links ) )
                echo '<li>…</li>';
        }
        /** Link to current page, plus 2 pages in either direction if necessary */
        sort( $links );
        foreach ( (array) $links as $link ) {
            $class = $paged == $link ? ' class="active"' : '';
            printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
        }
        /** Link to last page, plus ellipses if necessary */
        if ( ! in_array( $max, $links ) ) {
            if ( ! in_array( $max - 1, $links ) )
                echo '<li>…</li>' . "\n";

            $class = $paged == $max ? ' class="active"' : '';
            printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
        }
        /** Next Post Link */
        if ( get_next_posts_link() )
            printf( '<li>%s</li>' . "\n", get_next_posts_link() );
            echo '</ul></div>' . "\n";
    }
}

CSS:

div.navigation ul li{list-style:none;list-style-type:none;display:inline;padding:4px 4px;margin:4px 4px;border:1px solid #ccc;background-color:#eaeaea}
div.navigation ul li.active{background-color:#FC0;border:1px solid #C60}

现在,使用模板文件中的函数,如下所示:

if (function_exists('_numeric_posts_nav')) { _numeric_posts_nav(); }

如果帖子数量大于10

,它将显示分页