0
votes

I got a blog page that displays 10 posts followed by a navigation with "next" and "previous". But our client needs a numbered pagination and not just a simple page navigation.

The posts output comes from a post query and not from the standard wordpress option.

<section class="blog">

 <?php
 $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
 $args = array( 
 'posts_per_page' => 10, 
 'paged' => $paged, 
 'post_type' => 'post'
 );
 $cpt_query = new WP_Query($args);
?>

<?php if ($cpt_query->have_posts()) : while ($cpt_query->have_posts()) : $cpt_query->the_post(); ?>
 <div class="beitrag">
 <div class="beitrag-inhalt">


 <p><?php the_excerpt(); ?></p>
 </div>
      <div class="weiterlesen">
       <a href="<?php the_permalink(); ?>">Mehr Lesen</a>
      </div>
 </div>
 <?php endwhile; endif; ?>

<nav>
 <?php previous_posts_link( '<i class="fas fa-angle-left"></i> Vorherige Seite', $cpt_query->max_num_pages) ?>
 <?php next_posts_link( 'Nächste Seite <i class="fas fa-angle-right"></i>', $cpt_query->max_num_pages) ?>
</nav>

 </section>

How can I add page numbers to my navigation?

1

1 Answers

-1
votes

<?php  
$servername='localhost'
$username='root';
$password='';
$dbname = "my_db";
$conn=mysqli_connect($servername,$username,$password,"$dbname");
if(!$conn){
die('Could not Connect My Sql:' .mysql_error());
}
$limit = 10;  
if (isset($_GET["page"])) {
	$page  = $_GET["page"]; 
	} 
	else{ 
	$page=1;
	};  
$start_from = ($page-1) * $limit;  
$result = mysqli_query($conn,"SELECT * FROM user_table ORDER BY userid ASC LIMIT $start_from, $limit");
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</head>
<body>
<table class="table table-bordered table-striped">  
<thead>  
<tr>  
<th>userid</th>  
<th>First name</th>
<th>Last name</th>
 <th>City name</th>
<th>email</th> 
</tr>  
<thead>  
<tbody>  
<?php  
while ($row = mysqli_fetch_array($result)) {  
?>  
            <tr>  
				<td><?php echo $row["userid"]; ?></td>  
				<td><?php echo $row["first_name"]; ?></td>
				<td><?php echo $row["last_name"]; ?></td>
				<td><?php echo $row["city_name"]; ?></td>
				<td><?php echo $row["email"]; ?></td>			
            </tr>  
<?php  
};  
?>  
</tbody>  
</table>  
<?php  

$result_db = mysqli_query($conn,"SELECT COUNT(id) FROM user_table"); 
$row_db = mysqli_fetch_row($result_db);  
$total_records = $row_db[0];  
$total_pages = ceil($total_records / $limit); 
/* echo  $total_pages; */
$pagLink = "<ul class='pagination'>";  
for ($i=1; $i<=$total_pages; $i++) {
              $pagLink .= "<li class='page-item'><a class='page-link' href='pagination.php?page=".$i."'>".$i."</a></li>";	
}
echo $pagLink . "</ul>";  
?>

</body>
</html>