Wordpress

Some Tweaks For Paginated Posts in WordPress

Last time I posted about wordpress post pagination and incase you don’t know how to paginate the wordpress posts, then read this article first: Paginate WordPress Post – Breaking Posts Into Parts , After you have read this article then these tweaks might help you get some extra page views.

Tweaking the paginated posts - WordPress

Showing the total number of paginated parts/pages of a wordpress post:

This is quite easy and in a way it will acknowledge the user that the content which you are going to read has actually this number of parts. In order to show the total number of pages of a wordpress post, you need to edit the single.php of your theme. Go to your dashboard, then Appearance and from there go to editor.

WordPress stored the total number of paginated parts of a post into a variable, named as $numpages. So typically in the single.php you just need to give a global scope to this variable and then echo the value like this:

<?php global $numpages;
echo 'Total Pages of this post are '.$numpages; ?>

Showing the current page of the Paginated post in WordPress:

Same as showing the total number of pages you can also show the current number of page of the wordpress post. Like you can easily tell visitor that you are viewing the 1st page of the 24 pages. This will help you to get better pageviews. The current page number is stored in a variable, and you also need to make that global, named as $page.

<?php
global $page;
echo 'You are at '.$page;
?>

Showing a message user ends reading the whole paginated post:

You can easily use the if conditions using the $page and $numpages variables, to show something once user ends up reading the whole post through pagination:

<?php 
if ($page < $numpages) {
	# show user the post content
	the_content();
}	else {
	# show user a message...
	echo 'You have ended up reading this Paginated post, Consider read this:';
	$prev_post = get_next_post();
	echo '<a href="'.get_permalink( $prev_post->ID ).'">'.$prev_post->post_title.'</a>';
}
?>

In the above code, I am giving the condition that if the current page number is smaller than the total number of pages, then show the post content, but as soon as the current number of page becomes equal to total number of pages then kindly show user a message, “that you have completed reading the post, consider reading the next post“. But for this you may need to add an extra <!–nextpage–> tag at the end of your list, or post. Because normally the last part doesn’t have the <!–nextpage–> tag, but to use above code you will need to add an extra <!–nextpage–>

That was it!