You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<?phpglobal$wp_query; // you can remove this line if everything works for you// don't display the button if there are not enough postsif ( $wp_query->max_num_pages > 1 )
echo'<div class="misha_loadmore">More posts</div>'; // you can use <a> as well?>
Step 2. Enqueue jQuery and myloadmore.js. Pass query parameters to the script.
functionmisha_my_load_more_scripts() {
global$wp_query;
// In most cases it is already included on the page and this line can be removedwp_enqueue_script('jquery');
// register our main script but do not enqueue it yetwp_register_script( 'my_loadmore', get_stylesheet_directory_uri() . '/myloadmore.js', array('jquery') );
// now the most interesting part// we have to pass parameters to myloadmore.js script but we can get the parameters values only in PHP// you can define variables directly in your HTML but I decided that the most proper way is wp_localize_script()wp_localize_script( 'my_loadmore', 'misha_loadmore_params', array(
'ajaxurl' => site_url() . '/wp-admin/admin-ajax.php', // WordPress AJAX'posts' => json_encode( $wp_query->query_vars ), // everything about your loop is here'current_page' => get_query_var( 'paged' ) ? get_query_var('paged') : 1,
'max_page' => $wp_query->max_num_pages
) );
wp_enqueue_script( 'my_loadmore' );
}
add_action( 'wp_enqueue_scripts', 'misha_my_load_more_scripts' );
Step 3. myloadmore.js – what is inside?
Option 1. Load More button
jQuery(function($){$('.misha_loadmore').click(function(){varbutton=$(this),data={'action': 'loadmore','query': misha_loadmore_params.posts,// that's how we get params from wp_localize_script() function'page' : misha_loadmore_params.current_page};$.ajax({url : misha_loadmore_params.ajaxurl,// AJAX handlerdata : data,type : 'POST',beforeSend : function(xhr){button.text('Loading...');// change the button text, you can also add a preloader image},success : function(data){if(data){button.text('More posts').prev().before(data);// insert new postsmisha_loadmore_params.current_page++;if(misha_loadmore_params.current_page==misha_loadmore_params.max_page)button.remove();// if last page, remove the button// you can also fire the "post-load" event here if you use a plugin that requires it// $( document.body ).trigger( 'post-load' );}else{button.remove();// if no data, remove the button as well}}});});});
Option 2. No button, just load posts on scroll
jQuery(function($){varcanBeLoaded=true,// this param allows to initiate the AJAX call only if necessarybottomOffset=2000;// the distance (in px) from the page bottom when you want to load more posts$(window).scroll(function(){vardata={'action': 'loadmore','query': misha_loadmore_params.posts,'page' : misha_loadmore_params.current_page};if($(document).scrollTop()>($(document).height()-bottomOffset)&&canBeLoaded==true){$.ajax({url : misha_loadmore_params.ajaxurl,data:data,type:'POST',beforeSend: function(xhr){// you can also add your own preloader here// you see, the AJAX call is in process, we shouldn't run it again until completecanBeLoaded=false;},success:function(data){if(data){$('#main').find('article:last-of-type').after(data);// where to insert postscanBeLoaded=true;// the ajax is completed, now we can run it againmisha_loadmore_params.current_page++;}}});}});});
Step 4. wp_ajax_
functionmisha_loadmore_ajax_handler(){
// prepare our arguments for the query$args = json_decode( stripslashes( $_POST['query'] ), true );
$args['paged'] = $_POST['page'] + 1; // we need next page to be loaded$args['post_status'] = 'publish';
// it is always better to use WP_Query but not herequery_posts( $args );
if( have_posts() ) :
// run the loopwhile( have_posts() ): the_post();
// look into your theme code how the posts are inserted, but you can use your own HTML of course// do you remember? - my example is adapted for Twenty Seventeen themeget_template_part( 'template-parts/post/content', get_post_format() );
// for the test purposes comment the line above and uncomment the below one// the_title();endwhile;
endif;
die; // here we exit the script and even no wp_reset_query() required!
}
add_action('wp_ajax_loadmore', 'misha_loadmore_ajax_handler'); // wp_ajax_{action}add_action('wp_ajax_nopriv_loadmore', 'misha_loadmore_ajax_handler'); // wp_ajax_nopriv_{action}
Thank you for this great solution! I'm using this on a website, and when I updated to version 8.0 PHP it no longer works. Works fine on 7.4. Any ideas?
Thank you for this great solution! I'm using this on a website, and when I updated to version 8.0 PHP it no longer works. Works fine on 7.4. Any ideas?