Tutorial: Create own pagination in wordpress






in this tutorial we will create our own pagination in wordpress using “WP_Query” function.

I will do in my index.php file

Step 1 – Check the variable “page”

First thing we do is check whether via GET, we have a variable “page”. If no exist the variable, we start our pagination on page 1.

To ‘WP_Query’ function you can also pass parameters as the category. Review all the possible parameters here

 

$page=$_GET[‘page’];
if (isset($page)) {

$the_query = new WP_Query(‘posts_per_page=8&orderby=post_date&order=desc&paged=’.$page);

$numpost= $the_query->found_posts;

}else{
$the_query = new WP_Query(‘posts_per_page=8&orderby=post_date&order=desc&paged=1’);

$numpost= $the_query->found_posts;

}

Step 2 – Paging code

In this example I made the pagination for 8 entries per page. In addition, each entry will have a thumbnail (featured image) and an excerpt limited to 250 characters. This is the code:

while ($the_query->have_posts()) : $the_query->the_post(); ?>
<div class=”entry”>
<?php the_post_thumbnail(array(100,100), array (‘class’ => ‘imgnew’)); ?><a class=”title2″ title=”<?php the_title(); ?>” href=”<?php the_permalink() ?>” rel=”bookmark”><?php the_title(); ?></a> <br/>

<div class=”summary”>
<?php
$summary=get_the_excerpt();
echo substr($summary, 0, 250).”…”;
?>
</div>
<div style=”clear:both;”></div>
</div>
<?php endwhile; ?>
<?php
$currentpage=$_GET[‘page’];
if (!isset($currentpage)) {
$currentpage=1;
}else{
$currentpage=$_GET[‘page’];
}
if($numpost>8){
?><div class=”pagination”>
<?php
if($currentpage>1){
$previouspage=$currentpage-1;
echo ‘<div class=”previous”><a href=”?page=’.$previouspage.'”><< Previous page</a></div><br/>’;
}

$pages=$numpost/8;
if($currentpage<$pages){
$nextpage=$currentpage+1;
echo ‘<div class=”nextpage”><a href=”?page=’.$nextpage.'”>Next page >></a></div><br/>’;
}
?></div>
<?php
}
wp_reset_query(); ?>




 
Leave a Comment
Your comment: