如何在自定义插件列表页面中添加分页
How to add Pagination in custom plugin list page
我已经创建了一个用于从 WP 仪表板插入数据的插件。数据已成功插入和更新,我想在我的列表(视图)页面上添加分页。我是新开发者。这是我的列表页面代码。
<?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
}
试试这个代码。
<?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>';
}
}
已更新
我已经创建了一个用于从 WP 仪表板插入数据的插件。数据已成功插入和更新,我想在我的列表(视图)页面上添加分页。我是新开发者。这是我的列表页面代码。
<?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
}
试试这个代码。
<?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>';
}
}
已更新