-
-
Save stique77/1293bf1a23c1ae6f28408ca1b8099564 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* | |
* Javascript for Load More | |
* Added conditional to not display on blog home page or single post pages | |
* Added 'maxpage' to use in load-more.js to determine if there are no more posts | |
* | |
*/ | |
function be_load_more_js() { | |
global $wp_query; | |
// Don't show load more on single pages, posts, etc except for blog home page | |
if( !is_home() && is_singular() ) | |
return; | |
$args = array( | |
'nonce' => wp_create_nonce( 'be-load-more-nonce' ), | |
'url' => admin_url( 'admin-ajax.php' ), | |
'query' => $wp_query->query, | |
'maxpage' => $wp_query->max_num_pages, | |
); | |
wp_enqueue_script( 'be-load-more', get_stylesheet_directory_uri() . '/js/load-more.js', array( 'jquery' ), '1.0', true ); | |
wp_localize_script( 'be-load-more', 'beloadmore', $args ); | |
} | |
add_action( 'wp_enqueue_scripts', 'be_load_more_js', 9 ); |
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
// Modified version of http://www.billerickson.net/infinite-scroll-in-wordpress/#comment-674098. | |
// Adds spinner to show that it's working on loading more posts. | |
// Adds "Load More" pagination and when no more posts will display "No More Posts". | |
jQuery(function($){ | |
$('#genesis-content').append( '<div class="load-more"><span class="fa-spinner-hidden"><i class="fa fa-spinner fa-pulse fa-1x fa-fw"></i></span> MORE POSTS<span class="more-posts-arrow"></span></div>' ); | |
var button = $('#genesis-content .load-more'); | |
var page = 2; | |
var loading = false; | |
// Get max number of pages | |
var maxpage = beloadmore.maxpage; | |
// Remove Page links - .pagination | |
$('#genesis-content .pagination').remove(); | |
$('body').on('click', '.load-more', function(){ | |
$(".fa-spinner-hidden").fadeIn( "slow" ); | |
if( ! loading ) { | |
$(".fa-spinner-hidden").fadeOut( 1400 ); | |
loading = true; | |
var data = { | |
action: 'be_ajax_load_more', | |
nonce: beloadmore.nonce, | |
page: page, | |
query: beloadmore.query, | |
}; | |
$.post(beloadmore.url, data, function(res) { | |
if( res.success) { | |
// Add and Fade in new articles | |
$(res.data).hide().appendTo('#genesis-content').fadeIn(1000); | |
if( page >= maxpage) { | |
// If last page, remove "load more" button and replace with "No more articles" text. | |
$('#genesis-content .load-more').remove(); | |
$('#genesis-content').append( '<span class="no-more-posts">NO MORE POSTS</span>' ); | |
} else { | |
$('#genesis-content').append( button ); | |
} | |
page = page + 1; | |
loading = false; | |
} else { | |
// console.log(res); | |
} | |
}).fail(function(xhr, textStatus, e) { | |
// console.log(xhr.responseText); | |
}); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment