Skip to main content

How To Add (Create) Pagination in WordPress?

 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' => __('&laquo;'),

'next_text' => __('&raquo;'),

'total' => ceil($total / $post_per_page),

'current' => $page,

'type' => 'list'

));

echo '</div>';

?>

Comments

Popular posts from this blog

How to check date format in PHP?

Check date format in PHP function isCorrectDateFromat($date){     if(!empty($date)){         $dateString = $date; // Replace this with your date string         $format = "Y-m-d"; // Replace this with your expected date format         $dateTime = DateTime::createFromFormat($format, $dateString);         if ($dateTime === false) { /*             echo "The date is not in the correct format."; */         } else {             $errors = DateTime::getLastErrors();             if (empty($errors)) { /*                 echo "The date is in the correct format."; */                 return true;             } else { /*                 echo "...