我目前正在使用以下代码显示所有用户的列表。唯一的问题是我需要分页,一次只显示10个用户。我熟悉使用CPT和POST的分页,但不熟悉用户。
这可能吗?如果是,我从哪里开始?
<?php
/**
* @package WordPress
* @subpackage themename
* Template Name: Vendors Page
*/
get_header(); ?>
<?php
$url = $_SERVER[\'REQUEST_URI\'];
$tokens = explode(\'/\', $url);
$usernameCheck = $tokens[sizeof($tokens)-2];
if ($usernameCheck != \'vendors\') {
$username = $usernameCheck;
$username = str_replace("-", " ", $username);
}
if(isset($_POST[\'gform_submit\'])):
?>
<script>
jQuery(document).ready(function(){
jQuery(\'div.profileEdit\').show();
});
</script>
<? endif; ?>
<?php
$currentUserID = get_current_user_id();
$page = (!empty($_GET[\'userp\'])) ? $_GET[\'userp\'] : 1;
$per_page = 10;
$offset = ( ($page -1) * $per_page);
?>
<div id="main">
<div id="primary">
<div id="content">
<?php the_post(); ?>
<div class="contentLeft">
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?> role="article">
<h1 class="entry-title"><?php if (!empty($username)) {echo $username; ?> <a class="profileEdit">Click Here To Edit Your Profile</a><?php } else {the_title();} ?></h1>
<?php if (!empty($username)) { ?>
<?php
// WP_User_Query arguments
$args = array (
\'exclude\' => array( 1 ),
\'count_total\' => true,
\'fields\' => \'all_with_meta\',
\'meta_query\' => array(
array(
\'key\' => \'user_business\',
\'value\' => $username,
\'compare\' => \'LIKE\',
),
),
);
// The User Query
$user_query = new WP_User_Query( $args );
// The User Loop
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) { ?>
<?php if ( $currentUserID == $user->ID ) { ?><div class="profileEdit"><?php echo do_shortcode(\'[gravityform id="4" title="false" description="false"]\'); ?></div><?php } ?>
<div class="profileLeft">
<?php if (isset($user->user_pic)) { ?>
<img src="<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo $user->user_pic; ?>&w=235&h=147" title="<?php echo $user->user_business; ?>" alt="<?php echo $user->user_business; ?>" class="profilePic" />
<?php } else { $imgURL = get_template_directory_uri() . "/images/vendor_placeholder.jpg"; ?>
<img src="<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo $imgURL; ?>&w=235&h=147&zc=2" title="<?php echo $user->user_business; ?>" alt="<?php echo $user->user_business; ?>" class="profilePic" />
<?php } ?>
<h6>Owner Name:</h6>
<?php echo $user->first_name; ?> <?php echo $user->last_name; ?><br /><br />
<?php if ($user->user_address_show == \'Yes\') { ?>
<h6>Address:</h6>
<?php echo $user->user_address_street; ?><br /><?php echo $user->user_address_city; ?>, KS <?php echo $user->user_address_zip; ?><br /><br />
<?php } ?>
<h6>Phone:</h6>
<?php echo $user->user_phone; ?>
<a href="mailto:<?php echo $user->user_email; ?>" class="contactVendor">Contact Us Via Email</a>
<h6>Markets We Attend:</h6>
<?php if (isset($user->we_attend_east)) { ?><div class="location east"><strong>East Wichita</strong><br />Green Acres Market</div><?php } ?>
<?php if (isset($user->we_attend_west)) { ?><div class="location west"><strong>West Wichita</strong><br />Sedgwick County Ext. Office</div><?php } ?>
<?php if (isset($user->we_attend_delano)) { ?><div class="location delano"><strong>Delano in Wichita</strong><br />Lawrence Dumont Parking lot</div><?php } ?>
<?php if (isset($user->we_attend_derby)) { ?><div class="location derby"><strong>Derby, Kansas</strong></div><?php } ?>
<?php if (isset($user->attend_this_week_east) || isset($user->attend_this_week_wast) || isset($user->attend_this_week_delano) || isset($user->attend_this_week_derby)) { ?>
<div class="attend">We will be at the
<?php if (isset($user->attend_this_week_east)): ?>East Wichita Market<?php if (isset($user->attend_this_week_west) || isset($user->attend_this_week_delano) || isset($user->attend_this_week_derby)): ?>,<?php endif; ?><?php endif; ?>
<?php if (isset($user->attend_this_week_west)): ?>West Wichita Market<?php if (isset($user->attend_this_week_delano) || isset($user->attend_this_week_derby)): ?>,<?php endif; ?><?php endif; ?>
<?php if (isset($user->attend_this_week_delano)): ?>Delano in Wichita Market<?php if (isset($user->attend_this_week_derby)): ?><?php endif; ?><?php if (isset($user->attend_this_week_derby)): ?> and<?php endif; ?><?php endif; ?>
<?php if (isset($user->attend_this_week_derby)): ?>Derby, Kansas Market<?php endif; ?>
this week!
</div>
<?php } ?>
<h6>Items sold at market:</h6>
<ul class="profileItemsSold">
<?php if (isset($user->product_1)){ ?><li><?php echo $user->product_1; ?></li><?php } ?>
<?php if (isset($user->product_2)){ ?><li><?php echo $user->product_2; ?></li><?php } ?>
<?php if (isset($user->product_3)){ ?><li><?php echo $user->product_3; ?></li><?php } ?>
<?php if (isset($user->product_4)){ ?><li><?php echo $user->product_4; ?></li><?php } ?>
<?php if (isset($user->product_5)){ ?><li><?php echo $user->product_5; ?></li><?php } ?>
</ul>
<?php if (isset($user->user_organic)){ ?>
<div class="organic">Organic | Chemical Free</div>
<?php } ?>
<?php if (isset($user->user_producer)){ ?>
<div class="producer">100% Producer</div>
<?php } ?>
</div>
<div class="profileRight">
<h6>About Us:</h6>
<p><?php echo $user->user_about; ?></p>
</div>
<div class="profileBottom">
<?php if (isset($user->user_profile_pic)){ ?><h1 class="entry-title">View Our Photos</h1><?php } ?>
<?php if (isset($user->user_profile_pic)){ ?>
<div id="userPicsContainer">
<img src="<?php bloginfo(\'template_url\'); ?>/images/user_pics_prev.png" class="prev"/>
<img src="<?php bloginfo(\'template_url\'); ?>/images/user_pics_next.png" class="next"/>
<div class="targetDiv">
<ul>
<li><a href="<?php echo $user->user_profile_pic; ?>" class="fresco" data-fresco-caption="<?php echo $user->user_profile_pic_c; ?>"><img src="<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo $user->user_profile_pic; ?>&h=134&w=119" alt="<?php echo $user->user_profile_pic_c; ?>" title="<?php echo $user->user_profile_pic_c; ?>" /></a></li>
<?php if (isset($user->user_pic_1)){ ?><li><a href="<?php echo $user->user_pic_1; ?>" class="fresco" data-fresco-caption="<?php echo $user->user_pic_1_c; ?>"><img src="<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo $user->user_pic_1; ?>&h=134&w=119" alt="<?php echo $user->user_pic_1_c; ?>" title="<?php echo $user->user_pic_1_c; ?>" /></a></li><?php } ?>
<?php if (isset($user->user_pic_2)){ ?><li><a href="<?php echo $user->user_pic_2; ?>" class="fresco" data-fresco-caption="<?php echo $user->user_pic_2_c; ?>"><img src="<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo $user->user_pic_2; ?>&h=134&w=119" alt="<?php echo $user->user_pic_2_c; ?>" title="<?php echo $user->user_pic_2_c; ?>" /></a></li><?php } ?>
<?php if (isset($user->user_pic_3)){ ?><li><a href="<?php echo $user->user_pic_3; ?>" class="fresco" data-fresco-caption="<?php echo $user->user_pic_3_c; ?>"><img src="<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo $user->user_pic_3; ?>&h=134&w=119" alt="<?php echo $user->user_pic_3_c; ?>" title="<?php echo $user->user_pic_3_c; ?>" /></a></li><?php } ?>
<?php if (isset($user->user_pic_4)){ ?><li><a href="<?php echo $user->user_pic_4; ?>" class="fresco" data-fresco-caption="<?php echo $user->user_pic_4_c; ?>"><img src="<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo $user->user_pic_4; ?>&h=134&w=119" alt="<?php echo $user->user_pic_4_c; ?>" title="<?php echo $user->user_pic_4_c; ?>" /></a></li><?php } ?>
<?php if (isset($user->user_pic_5)){ ?><li><a href="<?php echo $user->user_pic_5; ?>" class="fresco" data-fresco-caption="<?php echo $user->user_pic_5_c; ?>"><img src="<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo $user->user_pic_5; ?>&h=134&w=119" alt="<?php echo $user->user_pic_5_c; ?>" title="<?php echo $user->user_pic_5_c; ?>" /></a></li><?php } ?>
<?php if (isset($user->user_pic_6)){ ?><li><a href="<?php echo $user->user_pic_6; ?>" class="fresco" data-fresco-caption="<?php echo $user->user_pic_6_c; ?>"><img src="<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo $user->user_pic_6; ?>&h=134&w=119" alt="<?php echo $user->user_pic_6_c; ?>" title="<?php echo $user->user_pic_6_c; ?>" /></a></li><?php } ?>
<?php if (isset($user->user_pic_7)){ ?><li><a href="<?php echo $user->user_pic_7; ?>" class="fresco" data-fresco-caption="<?php echo $user->user_pic_7_c; ?>"><img src="<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo $user->user_pic_7; ?>&h=134&w=119" alt="<?php echo $user->user_pic_7_c; ?>" title="<?php echo $user->user_pic_7_c; ?>" /></a></li><?php } ?>
<?php if (isset($user->user_pic_8)){ ?><li><a href="<?php echo $user->user_pic_8; ?>" class="fresco" data-fresco-caption="<?php echo $user->user_pic_8_c; ?>"><img src="<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo $user->user_pic_8; ?>&h=134&w=119" alt="<?php echo $user->user_pic_8_c; ?>" title="<?php echo $user->user_pic_8_c; ?>" /></a></li><?php } ?>
<?php if (isset($user->user_pic_9)){ ?><li><a href="<?php echo $user->user_pic_9; ?>" class="fresco" data-fresco-caption="<?php echo $user->user_pic_9_c; ?>"><img src="<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo $user->user_pic_9; ?>&h=134&w=119" alt="<?php echo $user->user_pic_9_c; ?>" title="<?php echo $user->user_pic_9_c; ?>" /></a></li><?php } ?>
</ul>
</div>
</div>
<?php } ?>
<?php if (isset($user->user_twitter) || isset($user->user_facebook)){ ?><h1 class="entry-title">Socialize With Us</h1><?php } ?>
<?php if (isset($user->user_twitter)){ ?>
<div id="twitterUserContainer">
<img src="<?php bloginfo(\'template_url\'); ?>/images/twitter_prev.png" class="prev"/>
<img src="<?php bloginfo(\'template_url\'); ?>/images/twitter_next.png" class="next"/>
<div class="targetDiv">
<?php echo do_shortcode(\'[twitter-user]\'); ?>
</div>
</div>
<br style="clear:both;" />
<?php } ?>
<?php if (isset($user->user_facebook)){ ?>
<iframe src="//www.facebook.com/plugins/likebox.php?href=<?php echo urlencode($user->user_facebook); ?>&width=588&height=590&show_faces=true&colorscheme=light&stream=true&show_border=true&header=true&appId=172839226207438" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:588px; height:590px;" allowTransparency="true"></iframe>
<?php } ?>
</div>
<?php } } ?>
<?php } else { ?>
<?php
// WP_User_Query arguments
$args = array (
\'exclude\' => array( 1 ),
\'count_total\' => true,
\'fields\' => \'all_with_meta\',
\'number\' => $per_page,
\'offset\' => $offset,
);
// The User Query
$user_query = new WP_User_Query( $args );
// The User Loop
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) { ?>
<div class="vendorContainer">
<a href="<?php bloginfo(\'url\'); ?>/vendors/<?php echo str_replace(" ", "-", strtolower($user->user_business)); ?>">
<?php if (isset($user->user_pic)) { ?>
<img src="<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo $user->user_pic; ?>&w=235&h=147" title="<?php echo $user->user_business; ?>" alt="<?php echo $user->user_business; ?>" />
<?php } else { $imgURL = get_template_directory_uri() . "/images/vendor_placeholder.jpg"; ?>
<img src="<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo $imgURL; ?>&w=235&h=147&zc=2" title="<?php echo $user->user_business; ?>" alt="<?php echo $user->user_business; ?>" />
<?php } ?></a>
<div class="vendorContent">
<h5><?php echo $user->user_business; ?><a href="<?php bloginfo(\'url\'); ?>/vendors/<?php echo str_replace(" ", "-", strtolower($user->user_business)); ?>" class="viewProfile">View Profile</a></h5>
<strong>Item(s) sold at market:</strong>
<ul><?php $productCounter = 0; ?>
<?php if (isset($user->product_1) && $productCounter == 0) { $productCounter = 1; ?><li><?php echo $user->product_1; ?></li><?php } ?>
<?php if (isset($user->product_2) && $productCounter == 0) { $productCounter = 1; ?><li><?php echo $user->product_2; ?></li><?php } ?>
<?php if (isset($user->product_3) && $productCounter == 0) { $productCounter = 1; ?><li><?php echo $user->product_3; ?></li><?php } ?>
<?php if (isset($user->product_4) && $productCounter == 0) { $productCounter = 1; ?><li><?php echo $user->product_4; ?></li><?php } ?>
<?php if (isset($user->product_5) && $productCounter == 0) { $productCounter = 1; ?><li><?php echo $user->product_5; ?></li><?php } ?>
</ul>
<strong>Market(s) we attend:</strong><?php $marketCounter = 0; ?>
<?php if (isset($user->we_attend_east) && $marketCounter != 2) { $marketCounter++ ?><div class="location east"><strong>East Wichita</strong><br />Green Acres Market</div><?php } ?>
<?php if (isset($user->we_attend_west) && $marketCounter != 2) { $marketCounter++ ?><div class="location west"><strong>West Wichita</strong><br />Sedgwick County Ext. Office</div><?php } ?>
<?php if (isset($user->we_attend_delano) && $marketCounter != 2) { $marketCounter++ ?><div class="location delano"><strong>Delano in Wichita</strong><br />Lawrence Dumont Parking lot</div><?php } ?>
<?php if (isset($user->we_attend_derby) && $marketCounter != 2) { $marketCounter++ ?><div class="location derby"><strong>Derby, Kansas</strong></div><?php } ?>
</div>
</div>
<?php } } ?>
<?php } ?>
<?php
$page_args = array(
\'base\' => get_permalink( get_the_ID() ). \'%_%\',
\'format\' => add_query_arg(array(\'userp\' => \'%#%\')),
\'total\' => ceil($users->total_users / $per_page),
\'current\' => $page,
\'show_all\' => True,
\'end_size\' => 2,
\'mid_size\' => 2,
\'prev_next\' => True,
\'prev_text\' => __(\'« Previous\'),
\'next_text\' => __(\'Next »\'),
\'type\' => \'plain\',
);
echo paginate_links($page_args);
?>
</article><!-- #post-<?php the_ID(); ?> -->
</div>
<?php if (!empty($username)) { ?><div class="contentRight"><?php if ( dynamic_sidebar(\'profile-sidebar\') ) : else : ?><?php endif; ?></div><?php } else { ?><?php get_sidebar(); ?><?php } ?>
</div>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
SO网友:s_ha_dum
以下是工作分页的解决方案:
$page = (!empty($_GET[\'userp\'])) ? $_GET[\'userp\'] : 1;
$per_page = 10;
$offset = ( ($page -1) * $per_page);
$args = array(
\'fields\' => \'all_with_meta\',
\'number\' => $per_page,
\'offset\' => $offset,
);
$users = new WP_User_Query( $args );
foreach( $users->results as $user ) {
// this is your foreach loop
// simplified for my convenience
echo $user->user_login;
echo \'<br>\';
}
$page_args = array(
\'base\' => get_permalink( get_the_ID() ). \'%_%\',
\'format\' => add_query_arg(array(\'userp\' => \'%#%\')),
\'total\' => ceil($users->total_users / $per_page),
\'current\' => $page,
\'show_all\' => True,
\'end_size\' => 2,
\'mid_size\' => 2,
\'prev_next\' => True,
\'prev_text\' => __(\'« Previous\'),
\'next_text\' => __(\'Next »\'),
\'type\' => \'plain\',
);
echo paginate_links($page_args);
如前所述,关键要素是偏移量和数字。
看看paginate_links
如需调整分页,请参阅参数说明。
看见WP_User_Query
如果需要调整查询本身。