Last active
December 29, 2017 19:07
-
-
Save bigredboots/279e27eeffc120534affd77b3730175c to your computer and use it in GitHub Desktop.
not working custom category infinite scroll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function be_load_it_js() { | |
$query = array( | |
'post__not_in' => array( get_queried_object_id() ), | |
'category_name' => ea_first_term( 'category', 'slug' ), | |
'posts_per_page' => 3 | |
); | |
$args = array( | |
'url' => admin_url( 'admin-ajax.php' ), | |
'query' => $query, | |
); | |
wp_enqueue_script( 'be-load-it', get_stylesheet_directory_uri() . '/js/load-moretwo.js', array( 'jquery' ), '1.0', true ); | |
wp_localize_script( 'be-load-it', 'beloadit', $args ); | |
} | |
add_action( 'wp_enqueue_scripts', 'be_load_it_js' ); | |
function be_ajax_load_it() { | |
$args = isset( $_POST['query'] ) ? array_map( 'esc_attr', $_POST['query'] ) : array(); | |
$args['post_type'] = isset( $args['post_type'] ) ? esc_attr( $args['post_type'] ) : 'post'; | |
$args['paged'] = esc_attr( $_POST['page'] ); | |
$args['post_status'] = 'publish'; | |
ob_start(); | |
$loop = new WP_Query( $args ); | |
if( $loop->have_posts() ): while( $loop->have_posts() ): $loop->the_post(); | |
echo '<h4><a href="' . get_permalink() . '">' . get_the_title() . '</a></h4>'; | |
endwhile; endif; wp_reset_postdata(); | |
$data = ob_get_clean(); | |
wp_send_json_success( $data ); | |
wp_die(); | |
} | |
add_action( 'wp_ajax_be_ajax_load_it', 'be_ajax_load_it' ); | |
add_action( 'wp_ajax_nopriv_be_ajax_load_it', 'be_ajax_load_it' ); | |
function ea_first_term( $taxonomy, $field ) { | |
$terms = get_the_terms( get_the_ID(), $taxonomy ); | |
if( empty( $terms ) || is_wp_error( $terms ) ) | |
return false; | |
// If there's only one term, use that | |
if( 1 == count( $terms ) ) { | |
$term = array_shift( $terms ); | |
} else { | |
$term = array_shift( $list ); | |
} | |
// Output | |
if( $field && isset( $term->$field ) ) | |
return $term->$field; | |
else | |
return $term; | |
} | |
JS: | |
jQuery(function($){ | |
$('.faqs').append( '<span class="load-it" style="background:red; height:5px; display:block"></span>' ); | |
var button = $('.faqs .load-it'); | |
var page = 2; | |
var loading = false; | |
var scrollHandling = { | |
allow: true, | |
reallow: function() { | |
scrollHandling.allow = true; | |
}, | |
delay: 200 //(milliseconds) adjust to the highest acceptable value | |
}; | |
$(window).scroll(function(){ | |
if( ! loading && scrollHandling.allow ) { | |
scrollHandling.allow = false; | |
setTimeout(scrollHandling.reallow, scrollHandling.delay); | |
if ($(button).offset()) { | |
var offset = $(button).offset().top - $(window).scrollTop(); | |
} | |
if( 2000 > offset ) { | |
loading = true; | |
var data = { | |
action: 'be_ajax_load_it', | |
page: page, | |
query: beloadit.query, | |
nonce: beloadit.nonce, | |
}; | |
$.post(beloadit.url, data, function(res) { | |
if( res.success) { | |
console.log (res) | |
$('.faqs').append( res.data ); | |
$('.faqs').append( button ); | |
page = page + 1; | |
loading = false; | |
} else { | |
console.log('res' + res); | |
} | |
}).fail(function(xhr, textStatus, e) { | |
console.log('xhr' + xhr.responseText); | |
}); | |
} | |
} | |
}); | |
}); | |
HTML-------------- | |
<div class="faqs"> | |
<ul class="faqs__list"> | |
<?php while (have_posts()) : the_post(); ?> | |
<li class="faqs__list__item"> | |
<a href="<?php the_permalink(); ?>" class="black-text medium-text"><?php the_title(); ?></a> | |
</li> | |
<?php endwhile; ?> | |
</ul> | |
<div class="nav-links"> | |
<span class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></span> | |
<span class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></span> | |
</div> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment