分页结果在我的插件中

时间:2012-12-03 18:44:23

标签: wordpress-plugin wordpress

我正在制作一个插件,显示某些用户操作的日志。

问题是我需要添加分页,以便每页只显示5个结果,而不是如何使用paginate_links()函数。

这是我的代码(我需要添加分页)):

$mylink = $wpdb->get_results("SELECT * FROM wp_points");

foreach ($milink as $mostrar) 
{

<tr>

echo"<td>".$mostrar->punto_user_ID."</td>
<td>".$mostrar->punto_nombre."</td>
<td>".number_format($mostrar->punto_canjeados, 0, ',', '.')."</td>
<td>".cambiarFormatoFecha($mostrar->punto_fecha)."</td>";

}
echo"</tr>
</tbody>
</table>";

请任何可以提供帮助的人。

谢谢!

2 个答案:

答案 0 :(得分:1)

这是一个可行的快速解决方案

使用的功能

小区: http://php.net/manual/en/function.ceil.php

wpdb-&GT;准备 http://codex.wordpress.org/Class_Reference/wpdb

分页链接 http://codex.wordpress.org/Function_Reference/paginate_links

$per_page = 5;
$page = intval(get_query_var('page')); // cast to int to be on the safe side
$total_pages = ceil($wpdb->get_var("SELECT COUNT(*) FROM wp_points") / $per_page);  


//use $wpdb->prepare to help against sql injection
$sql  = $wpdb->prepare("SELECT * FROM wp_points LIMIT %d, %d", $page * $per_page, $per_page);


// your processing here 

$mylink = $wpdb->get_results($sql);
//...

// here's the helper no magick here, straight from docs
$big=999999999; // dummy used by 'base' below

echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?page=%#%',
    'current' => max( 1, $page ),
    'total' => $total_pages,
) );

答案 1 :(得分:0)

最好看一下wp_query(“分页参数”位于页面的中间位置) 使用类似的东西:

$query = new WP_Query( 'posts_per_page=5' );
相关问题