WP_USER_QUERY上的分页不更新当前页面

时间:2013-07-12 作者:Austin Biggs

我有以下代码,可以查询所有用户(不包括admin)并按meta value user\\u business对其进行排序。当有10个以上的用户时,我的分页当前工作正常,除非“userp”等于什么,否则分页会将第1页始终设置为当前页。有人能帮我纠正这个问题吗?

    <?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);
    }
?>
<?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 str_replace("%27", "\'", $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\'     => str_replace("%27", "\'", $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 ): ?><a href="<?php bloginfo(\'url\'); ?>/update-your-profile/" class="profileEdit">Click Here To Edit Your Profile</a><?php endif; } } } ?></h1>

                    <?php if (!empty($username)) { ?> 
                     <?php wp_reset_query(); ?> 
                    <?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\'     => str_replace("%27", "\'", $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 ) { ?>
                            <div class="profileLeft">
                                <?php if (isset($user->user_profile_pic)) { ?>
                                                <img src="<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo $user->user_profile_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; ?>&nbsp;<?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; ?>,&nbsp;KS&nbsp;<?php echo $user->user_address_zip; ?><br /><br />
                                <?php } ?>

                                <?php if (isset($user->user_phone)){ ?>
                                    <h6>Phone:</h6>
                                    <?php echo $user->user_phone; ?>
                                <?php } ?>

                                <?php if (isset($user->user_booth)){ ?>
                                    <h6>Booth #:</h6>
                                    <?php echo $user->user_booth; ?>
                                <?php } ?>

                                <?php if (isset($user->website)){ ?>
                                    <a href="<?php echo $user->website; ?>" class="profileWebsite" target="_blank">View Our Website</a>
                                    <?php echo $user->website; ?>
                                <?php } ?>

                                <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)): ?>&nbsp;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 } ?>
                                </ul>

                                <?php if ($user->user_organic == \'yes\'){ ?>
                                    <div class="organic">Organic | Chemical Free</div>
                                <?php } ?>

                                <?php if ($user->user_producer == \'yes\'){ ?>
                                    <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); ?>&amp;width=588&amp;height=590&amp;show_faces=true&amp;colorscheme=light&amp;stream=true&amp;show_border=true&amp;header=true&amp;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,
                                \'meta_key\'       => \'user_business\',
                                \'query_id\'       => \'wps_users\',
                            );


                            // 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 } ?>
<div class="pagination">                
                <?php
                $page_args = array(
    \'base\'         => \'%_%\',
    \'format\'       => add_query_arg(array(\'userp\' => \'%#%\')),
    \'total\'        => ceil($user_query->total_users / $per_page),
    \'current\'      => $page,
    \'end_size\'     => 2,
    \'mid_size\'     => 2,
    \'prev_next\'    => True,
    \'prev_text\'    => __(\'&#171; Previous\'),
    \'next_text\'    => __(\'Next &#187;\'),
    \'type\'         => \'plain\',
);

echo paginate_links($page_args);
?>
</div>
                </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(); ?>

2 个回复
SO网友:s_ha_dum

我运行了你的代码,我看到的问题似乎与你描述的相反——第1页never 设置为当前页面。假设这就是问题所在,我理解这个困惑。这个问题很难解决。您需要设置base 到绝对URL。

\'base\' => get_permalink( get_the_ID() ). \'%_%\',
或者类似的东西,在你需要的环境下工作。也就是说,get_the_ID() 不适用于所有环境。但绝对URL是关键。

SO网友:Austin Biggs

在使用$page变量之后,我发现只要在paginate\\u links之前重新分配$page变量就可以解决所有问题。

    <?php
                $page = $_GET[\'userp\'];
                $page_args = array(
    \'base\'         => \'%_%\',
    \'format\'       => add_query_arg(array(\'userp\' => \'%#%\')),
    \'total\'        => ceil($user_query->total_users / $per_page),
    \'current\'      => $page,
    \'end_size\'     => 2,
    \'mid_size\'     => 2,
    \'prev_next\'    => True,
    \'prev_text\'    => __(\'&#171; Previous\'),
    \'next_text\'    => __(\'Next &#187;\'),
    \'type\'         => \'plain\',
);

echo paginate_links($page_args);
?>

结束

相关推荐

按自定义字段对GET_USERS排序

我有一个名为“label”的自定义用户分类法,其中还有一个自定义字段“sort\\u order”-我试图返回具有标签分类法的用户列表,然后按sort\\u order字段对该列表进行排序。到目前为止,我。。。。。<?php $users = get_users( array( \'meta_key\' => \'sort_order\', \'fields\' => \'all_with_meta\' ) ); &