I am using the JQuery infinite scroll script on my Wordpress website. The script works fine, but I cannot get the script to stop once all posts are shows. I am using it on a custom post type, and making a query directly to the wordpress (MySQL) database.
I have also created a custom pagination to use with the infinite scroll, and use $Totalpages
to check how many pages are available. Once all the Posts have been shown the $Totalpages
variable equals "0" and I know that there is no more posts available to show and infinite scroll should stop working. Here is the JavaScript inside my page...
<script>
var infinite_scroll = {
loading: {
img: "<?php echo get_template_directory_uri(); ?>/images/ajax-loader.gif",
msgText: "<?php _e( 'Loading the next set of posts...', 'custom' ); ?>",
finishedMsg: "<?php _e( 'All posts loaded.', 'custom' ); ?>"
},
"nextSelector":".previous a",
"navSelector":".post-nav",
"itemSelector":"article",
"contentSelector":".main"
};
jQuery( infinite_scroll.contentSelector ).infinitescroll( infinite_scroll );
</script>
I was thinking to stop the JQuery from continuing to excecute by
if ($Totalpages == 0) { stop Jquery from executing}
and when I need to execute it to write if ($Totalpages !=0){Execute the Javascript}
how will I go about implementing this.
UPDATE TO QUESTION
Ok I have updated my problem to what Ceryl suggested below, modifying the code to fit the code above
<?php if ($total_pages == 0) {
echo 'jQuery(window).unbind( infinite_scroll.contentSelector ).infinitescroll( infinite_scroll );';
}?>
<?php if ($total_pages != 0){
echo 'jQuery(window).bind( infinite_scroll.contentSelector ).infinitescroll( infinite_scroll );';
//$(window).unbind('.infscr');
}?>
The if statements seem correct but when I implement it on my page it does not work. If I set the unbind option to 4 (which is the total number of pages I have it stops the JQuery from binding and so I know it works). But when implementing it, it does not!
Any ideas why this is?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…