问题
I have created a plugin for data insert from WP dashboard. Data has inserted & updated successfully, I want to add a pagination on my list(view)page. I am new developer. this my list-page code.
<?php
function wp_schools_list() {
?>
<link type="text/css" href="<?php echo WP_PLUGIN_URL; ?>mywp/sinetiks-schools/style-admin.css" rel="stylesheet" />
<div class="wrap abs">
<h2>Schools</h2>
<div class="tablenav top">
<div class="alignleft actions">
<a href="<?php echo admin_url('admin.php?page=mywp_schools_create'); ?>">Add New</a>
</div>
<br class="clear">
</div>
<?php
$path_array = wp_upload_dir()['baseurl']; // wp_upload_dir has diffrent types of array I am used 'baseurl' for path
global $wpdb;
$table_name = $wpdb->prefix . "school";
$rows = $wpdb->get_results("SELECT id,code,name,image_name from $table_name");
?>
<table class='wp-list-table widefat fixed striped posts'>
<tr>
<th class="manage-column ss-list-width">ID</th>
<th class="manage-column ss-list-width">Code</th>
<th class="manage-column ss-list-width">Name</th>
<th class="manage-column ss-list-width">Image</th>
<th> </th>
<th> </th>
</tr>
<?php foreach ($rows as $row) { ?>
<tr>
<td class="manage-column ss-list-width"><?php echo $row->id; ?></td>
<td class="manage-column ss-list-width"><?php echo $row->code; ?></td>
<td class="manage-column ss-list-width"><?php echo $row->name; ?></td>
<td><img src="<?php echo $path_array; ?>/2016/12/<?php echo $row->image_name;?> " width="100" height="100" /> </td>
<td><a href="<?php echo admin_url('admin.php?page=wp_schools_update&id=' . $row->id); ?>">Update</a></td>
<td><a href="<?php echo admin_url('admin.php?page=wp_schools_delete&id=' . $row->id); ?>">Delete</a></td>
</tr>
<?php } ?>
</table>
</div>
<?php
}
回答1:
Try this code.
<?php
function wp_schools_list() {
global $wpdb;
$table_name = $wpdb->prefix . "school";
$pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;
$limit = 10; // number of rows in page
$offset = ( $pagenum - 1 ) * $limit;
$total = $wpdb->get_var( "select count(*) as total from $table_name" );
$num_of_pages = ceil( $total / $limit );
$rows = $wpdb->get_results( "SELECT id,code,name,image_name from $table_name limit $offset, $limit" );
$rowcount = $wpdb->num_rows;
?>
<link type="text/css" href="<?php echo WP_PLUGIN_URL; ?>mywp/sinetiks-schools/style-admin.css" rel="stylesheet" />
<div class="wrap abs">
<h2>Schools</h2>
<div class="tablenav top">
<div class="alignleft actions">
<a href="<?php echo admin_url('admin.php?page=mywp_schools_create'); ?>">Add New</a>
</div>
<br class="clear">
</div>
<?php
$path_array = wp_upload_dir()['baseurl']; // wp_upload_dir has diffrent types of array I am used 'baseurl' for path
?>
<table class='wp-list-table widefat fixed striped posts'>
<tr>
<th class="manage-column ss-list-width">ID</th>
<th class="manage-column ss-list-width">Code</th>
<th class="manage-column ss-list-width">Name</th>
<th class="manage-column ss-list-width">Image</th>
<th> </th>
<th> </th>
</tr>
<?php
if($rowcount>0){
foreach ($rows as $row) { ?>
<tr>
<td class="manage-column ss-list-width"><?php echo $row->id; ?></td>
<td class="manage-column ss-list-width"><?php echo $row->code; ?></td>
<td class="manage-column ss-list-width"><?php echo $row->name; ?></td>
<td><img src="<?php echo $path_array; ?>/2016/12/<?php echo $row->image_name;?> " width="100" height="100" /> </td>
<td><a href="<?php echo admin_url('admin.php?page=wp_schools_update&id=' . $row->id); ?>">Update</a></td>
<td><a href="<?php echo admin_url('admin.php?page=wp_schools_delete&id=' . $row->id); ?>">Delete</a></td>
</tr>
<?php } }else{
echo "<tr><td cols=an='5'>No records found</td></tr>";
} ?>
</table>
</div>
<?php
$page_links = paginate_links( array(
'base' => add_query_arg( 'pagenum', '%#%' ),
'format' => '',
'prev_text' => __( '«', 'text-domain' ),
'next_text' => __( '»', 'text-domain' ),
'total' => $num_of_pages,
'current' => $pagenum
) );
if ( $page_links ) {
echo '<div class="tablenav" style="width: 99%;"><div class="tablenav-pages" style="margin: 1em 0">' . $page_links . '</div></div>';
}
}
Updated
来源:https://stackoverflow.com/questions/41239282/how-to-add-pagination-in-custom-plugin-list-page