问题
I have a code of a plugin I made, which shows a custom table and I need to add paging.
This code does appear pagination and results, but does not work when clicking on the following pages (1, 2, 3 ... etc) but shows the same.
This is my code:
$per_page = 3;
$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_puntos_log") / $per_page);
//use $wpdb->prepare to help against sql injection
$sql = $wpdb->prepare("SELECT * FROM wp_puntos_log LIMIT %d, %d", $page * per_page, $per_page);
$mylink = $wpdb->get_results($sql);
foreach ($mylink as $mostrar)
{
echo"
<tr>
<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>";
$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,
) );
Took a long time trying and not working. I appreciate your help.
Greetings!
回答1:
Edit: ( I found the problem in the original answer i was missing $ sign
$sql = $wpdb->prepare("SELECT * FROM wp_puntos_log LIMIT %d, %d", $page * per_page, $per_page);
should be:
$sql = $wpdb->prepare("SELECT * FROM wp_puntos_log LIMIT %d, %d", $page * $per_page, $per_page);
回答2:
<?php
/* Plugin Name: Pagination */
function pag_test() {
global $wpdb;
echo "<h1> Custom Pagination Example </h1>";
$per_page = 3;
$page = intval($_GET['paged']); // cast to int to be on the safe side
$total_pages = floor($wpdb->get_var("SELECT COUNT(*) FROM wp_options") / $per_page);
//use $wpdb->prepare to help against sql injection
$sql = $wpdb->prepare("SELECT * FROM wp_options LIMIT %d, %d", max($page-1, 0) * $per_page, $per_page);
$mylink = $wpdb->get_results($sql);
?> <table>
<thead>
<tr>
<th> Option </th>
<th> Value </th>
</tr>
<?php foreach ($mylink as $mostrar)
{
echo"
<tr>
<td>".$mostrar->option_name."</td>
<td>".$mostrar->option_value."</td>
";}
echo" </tr>
</tbody>
</table>";
$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,
) );
}
function pag_addmenu() {
add_menu_page('Pagination', 'pagination', 'manage_options', 'pgtest', pag_test);
}
add_action('admin_menu', 'pag_addmenu');
?>
来源:https://stackoverflow.com/questions/13727000/i-can-not-paginate-my-plugin