我无法对我的插件进行分页

时间:2012-12-05 15:45:20

标签: wordpress-plugin wordpress

我有一个我制作的插件代码,它显示了一个自定义表,我需要添加分页。

此代码确实显示分页和结果,但在单击以下页面(1,2,3 ......等)时不起作用,但显示相同。

这是我的代码:

$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,
) );

花了很长时间尝试而不工作。感谢您的帮助。

问候!

2 个答案:

答案 0 :(得分:0)

编辑:( 我在原始答案中发现问题我错过了$ sign

$sql  = $wpdb->prepare("SELECT * FROM wp_puntos_log LIMIT %d, %d", $page * per_page, $per_page);

应该是:

$sql  = $wpdb->prepare("SELECT * FROM wp_puntos_log LIMIT %d, %d", $page * $per_page, $per_page);

答案 1 :(得分:0)

<?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');
?>