Last active
December 31, 2015 17:59
-
-
Save EmranAhmed/8023471 to your computer and use it in GitHub Desktop.
WordPress Infinit 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 | |
/** | |
* Infinite Scroll on the Homepage | |
* | |
* Display posts until the end of time | |
* | |
* @package WordPress | |
* @subpackage Lossip | |
* @since Lossip 1.0 | |
*/ | |
class mmg_infinite_scroll { | |
var $is_infinite_scroll = false; | |
var $posts_per_page = 10; // posts_per_page passed to the WP_Query - MUST REMAIN INTEGER | |
var $batch_offset = 10; // offset passed to the WP_Query - MUST REMAIN INTEGER | |
var $content_container = 'infinite-scroll'; // Container ID to append new articles to | |
var $pixel_offset = '500'; // How many pixels from the bottom of the scroll before triggering IS | |
var $category_not_in = array( 12135, 2605 ); // category__not_in passed to the WP_Query - MUST REMAIN INTEGER | |
var $firstrun = true; | |
// Actions and filters | |
function __construct() { | |
add_action( 'wp_footer', array( $this, 'infinite_scroll_script' ) ); | |
add_action( 'parse_request', array( $this, 'infinite_scroll_process_request' ) ); | |
add_filter( 'query_vars', array( $this, 'infinite_scroll_query_vars' ) ); | |
} | |
// Register the infinite-scroll-batch query var | |
function infinite_scroll_query_vars( $vars ) { | |
$vars[] = 'infinite-scroll-batch'; | |
return $vars; | |
} | |
// Decide whether or not this is an infinite scroll request and call do_infinite_scroll | |
function infinite_scroll_process_request( $wp ) { | |
if ( array_key_exists( 'infinite-scroll-batch', $wp->query_vars ) ) { | |
$this->is_infinite_scroll = true; | |
$this->batch_offset = $wp->query_vars['infinite-scroll-batch']; | |
$this->do_infinite_scroll(); | |
} | |
} | |
// Add infinite scroll javascript handling to home page header | |
function infinite_scroll_script() { | |
if( is_home() && is_front_page() ) : ?> | |
<script type="text/javascript"> | |
jQuery(document).ready(function($) { | |
$('.pagination').remove(); | |
var offset=<?php echo $this->batch_offset; ?>; | |
var inProgress=false; | |
$(document).scroll( function() { | |
//calculate our offsets. | |
if(document.documentElement.clientHeight+$(document).scrollTop()>=document.body.offsetHeight-<?php echo $this->pixel_offset; ?>&&inProgress==false) { | |
inProgress=true; | |
$('#<?php echo $this->content_container; ?>').append( '<div class="loading-indicator"><img src="<?php bloginfo( 'template_url' ); ?>/images/loading.gif"></div>'); | |
$.get( '<?php bloginfo( 'url' ); ?>/?infinite-scroll-batch='+offset,function(data) { | |
$('#<?php echo $this->content_container; ?>').append(data);_gaq.push(['_trackPageview','infinite-scroll-batch-'+offset]);offset=offset+<?php echo $this->batch_offset; ?>; | |
inProgress=false; | |
$('.loading-indicator').each(function(){$(this).hide();}); | |
}); | |
} | |
}); | |
}); | |
</script> | |
<?php endif; | |
} | |
// HTML code that is output during an infinite scroll request | |
function do_infinite_scroll() { | |
if( $this->is_infinite_scroll ) { | |
//offset of 4 to get past the first 4 posts. Done once only | |
if ( $this->firstrun ) { | |
$offset = $this->batch_offset + 4; | |
$this->firstrun = false; | |
} else { | |
$offset = $this->batch_offset; | |
} | |
$batch = new WP_Query( array( | |
'posts_per_page' => $this->posts_per_page, | |
'offset' => $offset, | |
'category__not_in' => $this->category_not_in, | |
'post__not_in' => lo_get_content_option( 'home_featured' ) | |
)); | |
$postindex = 1; | |
if ( $batch->have_posts() ): while ( $batch->have_posts() ): $batch->the_post(); | |
global $post; | |
// If on the 4th post of the loop, display a larger image | |
if ( 4 == $postindex) { ?> | |
<article <?php post_class( 'post-'. $postindex .'' ); ?>> | |
<div class="left"> | |
<a href="<?php the_permalink(); ?>"> | |
<?php the_post_thumbnail( 'lo-medium' ); ?> | |
</a> | |
</div> | |
<div class="right"> | |
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> | |
<div class="meta"> | |
<span class="date"><?php the_time( 'M j, Y' ); ?></span> | |
<span class="tags"><?php the_tags( '', ' , ', '' ); ?></span> | |
</div> | |
<p><?php lo_readwatch_more(); ?></p> | |
<div class="comment-count"> | |
<a href="<?php the_permalink(); ?>#comments"><?php comments_number( '0 Comments', '1 Comment', '% Comments' ); ?></a> | |
</div> | |
</div> | |
</article><!-- .post --> | |
<?php | |
// Otherwise, loop through the posts normally | |
} else { ?> | |
<article <?php post_class( 'post-'. $postindex .'' ); ?>> | |
<div class="left"> | |
<div class="video-overlay-home-loop-container"> | |
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'lo-thumb' ); ?></a> | |
<?php if ( in_category( 'video' ) ) : ?><a class="playIcon" href="<?php the_permalink(); ?>"><?php _e( 'Play', 'lossip' ); ?></a><?php endif; ?> | |
</div> | |
<div class="comment-count"> | |
<a href="<?php the_permalink(); ?>#comments"><?php comments_number( '0 Comments', '1 Comment', '% Comments' ); ?></a> | |
</div> | |
</div> | |
<div class="right"> | |
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> | |
<div class="meta"> | |
<span class="date"><?php the_time('M j, Y'); ?></span> | |
<span class="tags"><?php the_tags( '', ' , ', '' ); ?></span> | |
</div> | |
<?php lo_readwatch_more(); ?> | |
</div> | |
</article><!-- .post --> | |
<?php | |
// If on the 7th post in the loop, display Panorama Advertisement | |
} if ( 7 == $postindex && $code = lo_get_ad_option( 'home_panorama' ) ): ?> | |
<div class="panorama"> | |
<?php echo $code; ?> | |
</div> | |
<?php endif; ?> | |
<?php $postindex++; endwhile; endif; wp_reset_postdata(); | |
if ( get_option( 'advertising_enable_infinitescroll' ) == 'on' ) echo '<iframe style="margin-bottom:20px;" src="http://poweredby.moguldom.com/ads/pages/mmg-los-is.html" width="643" height="250" marginheight="0" marginwidth="0" frameborder="0" scrolling="no"></iframe>'; | |
die(); | |
} | |
} | |
} | |
$azwethinkweiz = new mmg_infinite_scroll(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment