Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Twitter like next post load for Wordpress
<?php
add_action('wp_ajax_and_action', 'add_next_post');
add_action('wp_ajax_nopriv_and_action', 'add_next_post');
function add_next_post()
{
$pagged = $_POST['paged'];
query_posts("paged=$pagged&post_status=publish");
get_template_part('loop', 'index');
die();
}
add_filter('wp_footer', 'javascript_page');
function javascript_page() {
global $query_string;
?><script type="text/javascript">
jQuery(document).ready(function($) {
var ajaxurl = '<?php bloginfo('siteurl') ?>/wp-admin/admin-ajax.php';
var page_number = 2;
var busy = false;
$(window).bind('scroll', function(e) {
if($(window).scrollTop() + $(window).height() > $(document).height() - 1000 && !busy)
{
busy = true;
$('.loading-more').html('Click to load more posts..');
$.post(ajaxurl, {
action: 'and_action',
paged: page_number
}, function(data) {
if(data == "")
{
$('.loading-more').html('<strong>No more posts!</strong>');
}
else
{
$('.empty-div').append(data);
busy = false;
page_number += 1;
}
});
}
});
$('.loading-more').bind('click', function(e) {
busy = true;
$('.loading-more').html('<em>Loading more posts..</em>')
$.post(ajaxurl, {
action: 'and_action',
paged: page_number
}, function(data) {
if(data == "") {
$('.loading-more').html('<strong>No more posts!</strong>');
}
else {
$('.empty-div').append(data);
busy = false;
page_number += 1;
}
});
});
});
</script>
<?php
}
// Add to index.php template
// <div class="empty-div"></div>
// <div class="loading-more"></div>
// http://phosphorus.co/2010/11/scroll-pagination/ - original code
@foodgy

This comment has been minimized.

Copy link

foodgy commented Jun 21, 2011

Not working for me :(
wordpress 3.1.3
Just shows "No more posts" after loading.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.