Demonstration
Solution
<ul>
<li><?php next_posts_link('« Older Entries') ?></li>
<li><?php previous_posts_link('Newer Entries »') ?></li>
</ul>
The code above is used to make the previous and next page for the posts. First we need to add an ID for the unordered list so we can add select it using jQuery later. Let’s give it ID ‘postPagination’.
<ul id='postPagination'>
<li><?php next_posts_link('« Older Entries') ?></li>
<li><?php previous_posts_link('Newer Entries »') ?></li>
</ul>
Now let’s make it work with ajax. Insert the code bellow in header.php file of your theme.
<script type="text/javascript" charset="utf-8">
jQuery(document).ready(function(){
jQuery('#postPagination a').live('click', function(e){
e.preventDefault();
var link = jQuery(this).attr('href');
jQuery('#content').html('Loading...');
jQuery('#content').load(link+' #contentInner');
});
});
</script>
Change the #content with ID of the div that wraps the #contentInner div of your page.
Here is how the layout looks.
<div id='content'>
<div id='contentInner'>
The Posts and the navigation are here
</div>
</div>
That’s it. Now when a link for previous or next post pages is clicked the new content will be loaded using ajax. If you are having troubles with these just say what’s your problem in the comments and i’ll do my best to help you.
Notice: Don’t forget to include the jQuery library inside the theme if it’s not already included.
Bonus example: Fading animation
Just change the javascript part to be like the snippet bellow.
jQuery(document).ready(function(){
jQuery('#postPagination a').live('click', function(e){
e.preventDefault();
var link = jQuery(this).attr('href');
jQuery('#content').fadeOut(500).load(link + ' #contentInner', function(){ jQuery('#content').fadeIn(500); });
});
});
That’s all. You can also download the files which include the fading animation.
Update:
You can now download the modified files. The files are from the “Classic” wordpress theme that comes with the wordpress download.
Update:
Improved code and added a “loading” message. (the download files are also updated).
Update:
Bonus example added with fadeIn and fadeOut. You can download the files from the example.
Our WordPress themes
We have a few WordPress themes that we would like you to take a look at if you want.
check out the portfolio











BTW, thanks for a great script. It works very well.
grrrrrrrrrrrr !!!!
cant type “script”…
hey,
when i clic next / prev post, it dont display the “script” included in it
except that, it works well with posts without “script”
Sorry, i don’t understand what you mean. And use < ; for < and > ; for > (without the space before ; )
yeah sorry
i have post contening code to display a slideshow from a website, this code start by <script > and i noticed those post not charging well when i use the ajax method to load the next or previous article
Ohh so inside of your post you have <script>, why did you do that, scripts don’t belong in the post content. Give me the URL i’ll see what i can do.
hi
how to implement this with mystique theme by digitalnature, under every post with a “read more” link instead of older and newer entries? mystique has a function called mystique_post and a pagenavi…with numbers…if you can edit or let me see an example about this, is really appreciate…thank you
ok. thanks
http://blackandmetisses.free.fr/blog/
look when you are into a post, when you switch to “post with object” to “post with script” using the links above the title, the post “post with script” is empty, ajax dont load the content
How about when the page you need to split into pagination is displaying content spat out from get_pages() ?
Thanks for sharing that! Is there any jquery solutions for permalinks and categories surfing?
Hi There,
Thanks for this code – very useful.
I was wondering if this would work on a custom page I am currently creating. Its basically a landing page for all my blog posts, set up in a 3 x 3 grid that pulls in a custom field image attached to each post.
Ideally I would like the user to be able to navigate backwards and forwards and this grid updates each time to display 9 newer / older posts.
Any thoughts greatly appreciated.
Mark
if you are able to do the same thing with last articles or last comments … or last …
you won’t be my hero. You’ll be my god
Tyring this for my blog.
Excellent. Worked almost first try.
mark it. very good. i am to try it.
I see the video. The entire content area is collapse while loading the next post. How to replace the effect with the image, like [img=http://www.ozsinopages.com/images/loading.gif]this[/img] image? And how to make the content area not collapses?
jQuery('#postPagination a').live('click', function(e){
04
e.preventDefault();
this really helps.”.click” just do not work,it bugs me a long time.
thanks!
I will use it for my next Theme.
tnx very much
you saved me real time
Best Regards …
Thx very much for the script. That’s exact what i was looking for!
Just one question. Is there any way to get the “back-button” of the browser working with that?
Best
Great script, but I’m having an issue; I have a jquery script that runs on my posts giving it an overlay on hover to show the excerpt and title. Once I click the older posts link it turns this script off and no long allows for the overlay to show on hover. Have any ideas on how I’d get that to work following the use of your script?
I’m testing this on a single page layout I’m working on. For testing I have 5 posts and trying to show two posts per page so that should be 3 pages (2 on page one, 2 on page three, and 1 on page 3).
I can go from page 1 to page 2 but the link never updates to paged=3 if I load it on page three I can go to 2 but the link never updates to paged=1.
Is there an important part that handles this that I’m missing?
Can’t make it work in IE8/IE7!
Have you faced any problem there?
Best Regards