Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
976 views
in Technique[技术] by (71.8m points)

wordpress - I can not paginate my plugin

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...