I'm confused as to what is happening here, I have 11 posts, with 3 initially loaded and 3 that load every time I scroll to the bottom of the page until all 11 are shown. This is fine.
The problem occurs when I click a button and dynamically load the first 3 posts onto the page again. This loaded via .html(data).
I'm doing this on a "page" called archives.
It's as though the archive page knows I displayed all 11 posts before and refuses to start again unless I refresh the page.
How do I reset the loop, or tell wordpress that I want to start from the 3rd post again?
jQuery(document).ready(function($){
$("a.sort-all").bind("click", function(e) {
load_posts(this);
e.preventDefault();
});
function load_posts() {
var ajax_url = $('.sort-all').attr('data-all-url');
$.ajax({
dataType: "HTML",
url: ajax_url,
type:'POST',
data:{
action: 'data_click_all',
offset: offset
},
success:function(data) {
$('#content').html(data);
}
});
}
var ajax_url = $('.sort-all').attr('data-all-url');
var offset = 0;
$('#content').waypoint(function(direction) {
if(direction === 'down'){
offset = parseInt(offset) + 3;
$.ajax({
dataType: "HTML",
url: ajax_url,
type:'POST',
data:{
action: 'data_scroll_all',
offset: offset
},
success:function(data) {
$('#content').append(data);
$.waypoints('refresh');
}
});
}
}, {
offset: 'bottom-in-view'
});
});
if (!class_exists('load_posts')) {
class load_posts {
/**
* PHP 4 Compatible Constructor
*/
function load_posts(){$this->__construct();}
/**
* PHP 5 Constructor
*/
function __construct(){
add_action('wp_ajax_data_scroll_all', array(&$this, 'data_scroll_all'));
add_action('wp_ajax_nopriv_data_scroll_all', array(&$this, 'data_scroll_all'));
//----//
add_action('wp_ajax_data_click_all', array(&$this, 'data_click_all'));
add_action('wp_ajax_nopriv_data_click_all', array(&$this, 'data_click_all'));
}
function data_click_all(){
global $post;
$offset = $_POST['offset'];
$args = array('posts_per_page' => 3, 'offset' => $offset, 'category__not_in' => 1,'orderby' => 'date');
$myposts = get_posts( $args );
foreach( $myposts as $post ) {
setup_postdata($post);
get_template_part( 'content-archive' );
} wp_reset_query();
die('');
}
function data_scroll_all(){
global $post;
$offset = $_POST['offset'];
$args = array('posts_per_page' => 3, 'category__not_in' => 1, 'offset' => $offset, 'orderby' => 'date');
$myposts = get_posts( $args );
foreach( $myposts as $post ) {
setup_postdata($post);
get_template_part( 'content-archive' );
} wp_reset_query();
die('');
}
}
}
if (class_exists('load_posts')) {
$newload_posts = new load_posts();
}
When you click a button and dynamically load the first 3 posts onto the page again, You have to reset the variable offset. Also refresh the waypoints.
$("a.sort-all").bind("click", function(e) {
offset=0;
load_posts(this);
$.waypoints('refresh');
e.preventDefault();
});
if it doesnt work, proably you dont have an error with the number of posts, but another error that prevents everything to work.
please, add an alert when you enter the waypoint
$('#content').waypoint(function(direction) {
alert("current: "+offset);
///...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.