如何将PHP分页添加到WordPress

时间:2013-11-13 作者:Abude

我正在尝试使用wordpress进行自定义PHP分页,wordpress中的页面是一个非常自定义的页面,不是帖子或页面,它是一个从DB中选择的显示花朵的页面,样式现在可以了,但它显示城市中的所有花朵,我想限制每页10朵花:

$per_page = 10;
$page = (isset($_GET[\'page\'])) ? (int)$_GET[\'page\'] : 1;
$startAt = $per_page * ($page - 1);

$user_query_subs = new WP_User_Query( array(\'meta_key\' => \'location\', \'number\' => $per_page, \'offset\' => $startAt, \'meta_value\'  => $flow_location, \'meta_compare\' => \'=\', \'role\' => \'admin\', \'orderby\' => \'rand\', \'order\' => \'DESC\'), array(\'fields\' => \'all_with_meta\') );


$all_user_query_subs = new WP_User_Query( array(\'meta_key\' => \'location\', \'meta_value\'  => $flow_location, \'meta_compare\' => \'=\', \'role\' => \'admin\', \'orderby\' => \'rand\', \'order\' => \'DESC\'), array(\'fields\' => \'all_with_meta\') );

$all_flowers = $all_user_query_subs->get_total();
$flowers = $user_query_subs->get_results();
加载内容后:我在底部添加链接,如下所示:

<?php
        $totalPages = $all_flowers / $per_page;
        $links = "";
        $current_url = "http://".$_SERVER[\'HTTP_HOST\'].$_SERVER[\'REQUEST_URI\'];
        for ($i = 1; $i <= $totalPages; $i++) {
        $links .= ($i != $page ) 
            ? "<a href=\'" . "$current_url" . "page/$i/\'>Page $i</a> "
            : "$page ";
}
    echo $links;
    ?>
select查询工作得很好,每页只加载10个,但我的问题是links 在底部:

现在,当我按第2页访问城市中接下来的10朵花时,我得到Page Not Found 从…起Wordpress 但是用正确的$current_url/page/$i

有什么想法吗?提前感谢!

1 个回复
SO网友:Hullen

您可以使用paginate_links() 通过wordpress核心API,可以更轻松地为自定义查询分页。您不需要再进行一次查询来统计所有用户。

<?php
$users_per_page = 10; // total no of author to display

$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
if( (int)$paged === 1 ){
  $offset = 0;
} else {
  $offset = ($paged-1) * $users_per_page;
}
$args = array(
    \'meta_key\' => \'location\',
    \'number\' => $users_per_page,
    \'offset\' => $offset,
    \'meta_value\'  => $flow_location,
    \'meta_compare\' => \'=\',
    \'role\' => \'admin\',
    \'orderby\' => \'ID\',
    \'order\' => \'DESC\'
);
$fields = array(\'fields\' => \'all_with_meta\');
$user_query_subs = new WP_User_Query( $args, $fields );
$users = $user_query_subs->get_results();
if ( !empty( $users ) ) {
    foreach ( $users as $user ) {
        // code goes here
    }
}
else {
    echo \'No flowers found.\';
}
?>
然后分页,记住这取决于你的结构
如果使用permalinks,则可以使用以下代码:

<?php
$query_string = $_SERVER[\'QUERY_STRING\']; // current query parameters
$total_users = $user_query_subs->total_users; // total number of users
$total_pages = ceil( $total_user/$users_per_page ); // total number of pages

// if on the front end, your base is the current page
$base = get_permalink( get_the_ID() ) . \'?\' . remove_query_arg(\'paged\', $query_string) . \'%_%\';
$paginate_args = array(  
    \'base\' => $base,  
    \'format\' => \'?paged=%#%\', // define the query parameter that will be used, in this case "paged" 
    \'current\' => $paged,
    \'total\' => $total_pages, 
    \'prev_text\' => \'Previous\',  
    \'next_text\' => \'Next\' ,
    \'end_size\' => 1,
    \'mid_size\' => 5
);
echo paginate_links($paginate_args); 
?>

结束

相关推荐

向users.php添加多个定制列

我正在尝试向用户添加两列。php文件。我不知道如何同时添加$total\\u time\\u display和$logged\\u in\\u amount\\u display。如何返回这两个值?这是我的代码:add_filter(\'manage_users_columns\', \'freeman_add_user_minutes_column\'); function freeman_add_user_minutes_column($columns) { $columns[