Add (Create) Pagination in WordPress.
<style>
.cus_pagination ul li .page-numbers {
background: white;
padding: 7px 11px;
margin-left: 8px;
text-decoration: none;
border: 1px solid black;
color: #000;
}
.cus_pagination ul {
display: flex;
}
.cus_pagination span.page-numbers.current {
background: #000;
color: white;
}
</style>
<?php
global $wpdb;
// QUERY HERE TO COUNT TOTAL RECORDS FOR PAGINATION
$cars = array
(
array("student" => "Jassi","marks" => "80"),
array("student" => "Jai","marks" => "90"),
array("student" => "Golu","marks" => "85"),
array("student" => "Ravinder","marks" => "30"),
array("student" => "Mukesh","marks" => "45"),
array("student" => "Ankush","marks" => "96"),
array("student" => "Aman","marks" => "90")
);
$total = count($cars);
$post_per_page = 1;
$page = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;
$offset = ( $page * $post_per_page ) - $post_per_page;
// QUERY HERE TO GET OUR RESULTS
//$results = $wpdb->get_results("SELECT student, marks FROM $cars LIMIT $post_per_page OFFSET $offset");
// or
$results = array_slice( $cars, $offset, $post_per_page );
/* print_R($results);
die();
*/
// PHP FOR EACH LOOP HERE TO DISPLAY OUR RESULTS
foreach($results as $row)
{
//print_r($row);
echo $row['student']." => ".$row['marks']."<br>";
}
// END OUR FOR EACH LOOP
?>
<?php
echo '<div class="cus_pagination">';
echo paginate_links( array(
'base' => add_query_arg( 'cpage', '%#%' ),
'format' => '',
'prev_text' => __('«'),
'next_text' => __('»'),
'total' => ceil($total / $post_per_page),
'current' => $page,
'type' => 'list'
));
echo '</div>';
?>
Comments
Post a Comment