I’m using the loop + jQuery below to load in the next set of pages, and it works on the first click, but when the next page is loaded in and I click on “newer posts” it reloads the whole page. Any ideas?
<div id="content">
<?php
$new_query = new WP_Query();
$new_query->query('post_type=post&showposts=1'.'&paged='.$paged);
?>
<?php while ($new_query->have_posts()) : $new_query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<div id="pagination">
<?php next_posts_link('« Older Entries', $new_query->max_num_pages) ?>
<?php previous_posts_link('Newer Entries »') ?>
</div>
</div><!-- #content -->
<script>
$('#pagination a').on('click', function(event){
event.preventDefault();
var link = $(this).attr('href'); //Get the href attribute
$('#content').fadeOut(500, function(){ });//fade out the content area
$('#content').load(link + ' #content', function() { });
$('#content').fadeIn(500, function(){ });//fade in the content area
});
</script>
You’re using jQuery’s
load()
method to insert content, which is a shortcut for$.ajax
, which of course inserts the content dynamically.Dynamic content requires delegation of the events to a non-dynamic parent, something jQuery makes easy with
on()
I used adeneo’s solution, with a couple of minor tweaks.
, so I performed a separate call for the pagination content. Per the comments, I’ve updated the code to make a single Ajax call, filtering on the returned data to retrieve and update the desired elements..disabled
class.display: none;
once the opacity animation is complete). Instead, I manually animate the opacity, which limits the amount of height fluctuation.Here’s the updated code: