escort sitesi alanya bayan escort manavgat bayan escort fethiye bayan escort kemer bayan escort didim bayan escort çanakkale bayan escort Aydın bayan escort muğla bayan escort tekirdağ bayan escort manisa bayan escort balıkesir bayan escort trabzon bayan escort elazığ bayan escort ordu bayan escort kütahya bayan escort ısparta bayan escort rize bayan escort maraş bayan escort yalova bayan escort giresun bayan escort yozgat bayan escort tokat bayan escort şanlıurfa bayan escort sivas bayan escort batman bayan escort erzurum bayan escort sinop bayan escort kırşehir bayan escort karaman bayan escort kırıkkale bayan escort bolu bayan escort amasya bayan escort niğde bayan escort uşak bayan escort edirne bayan escort çorum bayan escort osmaniye bayan escort zonguldak bayan escort van bayan escort erzincan bayan escort istanbul masaj ankara masaj izmir masaj bursa masaj antalya masaj adana masaj konya masaj şanlıurfa masaj gaziantep masaj kocaeli masaj mersin masaj diyarbakır masaj hatay masaj manisa masaj kayseri masaj samsun masaj balıkesir masaj maraş masaj van masaj aydın masaj tekirdağ masaj sakarya masaj denizli masaj muğla masaj adıyaman masaj afyon masaj ağrı masaj aksaray masaj amasya masaj ardahan masaj artvin masaj bartın masaj batman masaj bayburt masaj bilecik masaj bingöl masaj bitlis masaj bolu masaj burdur masaj çanakkale masaj çankırı masaj çorum masaj düzce masaj edirne masaj elazığ masaj erzincan masaj erzurum masaj eskişehir masaj giresun masaj gümüşhane masaj ığdır masaj ısparta masaj karabük masaj karaman masaj kastamonu masaj kilis masaj kırıkkale masaj kırklareli masaj kırşehir masaj uşak masaj kütahya masaj malatya masaj mardin masaj nevşehir masaj niğde masaj ordu masaj trabzon masaj osmaniye masaj rize masaj siirt masaj sinop masaj sivas masaj tokat masaj kayseri escort kayseri escort kıbrıs escort kilis escort konya escort konya escort konya escort malatya escort malatya escort malatya escort manisa escort gaziantep escort van escort tunceli escort bursa escort sivas escort siirt escort şanlıurfa escort bilecik escort samsun escort çanakkale escort istanbul escort ankara escort izmir escort izmir escort bursa escort antalya escort antalya escort adana escort kocaeli escort konya escort mersin escort şanlıurfa escort samsun escort diyarbakır escort hatay escort manisa escort balıkesir escort gaziantep escort sakarya escort tekirdağ escort kayseri escort eskişehir escort aydın escort van escort denizli escort kahramanmaraş escort izmit escort trabzon escort adıyaman escort afyon escort erzurum escort ordu escort bodrum escort bodrum escort malatya escort elazığ escort tokat escort sivas escort muğla escort aksaray escort amasya escort niğde escort batman escort kütahya escort çanakkale escort çankırı escort çorum escort düzce escort hakkari escort edirne escort yozgat escort zonguldak escort bingöl escort yalova escort uşak escort sinop escort şırnak escort rize escort muş escort nevşehir escort osmaniye escort bolu escort mardin escort kırşehir escort kastamonu escort giresun escort kırıkkale escort ısparta escort karabük escort karaman escort kibris escort hatay escort ağrı escort afyon escort antalya escort antalya escort ardahan escort artvin escort ankara escort balıkesir escort bartın escort ankara escort bayburt escort bitlis escort istanbul escort burdur escort
Load More Posts with AJAX. Step by Step Tutorial Without Plugins.

Load More Posts with AJAX. Step by Step Tutorial Without Plugins.

Load More Posts with AJAX. Step by Step Tutorial Without Plugins tutorial. It is something help you to execute this.

Step 1. Load more button #

You can skip this step if you want to load more posts on scroll.

We begin with the button HTML. Here is just one main rule – do not show the button if there are not enough posts. We will check it with $wp_query->max_num_pages.

global $wp_query; // you can remove this line if everything works for you
// don't display the button if there are not enough posts
if ( $wp_query->max_num_pages > 1 )
echo '<div class="wp_loadmore">More posts</div>'; // you can use <a> as well

To style the button the according way use CSS below.

background-color: #ddd;
border-radius: 2px;
display: block;
text-align: center;
font-size: 14px;
font-size: 0.875rem;
font-weight: 800;
text-transform: uppercase;
padding: 10px 0;
transition: background-color 0.2s ease-in-out, border-color 0.2s ease-in-out, color 0.3s ease-in-out;
background-color: #767676;
color: #fff;

Step 2. Enqueue jQuery and myloadmore.js. Pass query parameters to the script. #

Well, that’s where the magic happens. This small piece of code allows you to pass the according parameters to the script that’s why the button can work on any page – tags, categories, custom post type archives, search etc. You can also use it for WooCommerce to load more products.

function wp_my_load_more_scripts() {

global $wp_query;

// In most cases it is already included on the page and this line can be removed

// register our main script but do not enqueue it yet
wp_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', 'wp_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', 'wp_my_load_more_scripts' );

If you do not know, the above code is for your functions.php. And please note, my load more button is adapted for the main loop only. If you need it for a custom loop, you can.

You can also pass ajaxurl like this admin_url( ‘admin-ajax.php’ ).

Step 3. myloadmore.js – what is inside? #

It is a small JS file, actually you can place it anywhere you want, for simpleness I decided to place it just in a theme directory (line 9 of previous code).

I also think that I should give you a choice – a load more button or just load posts by scroll.

With the help of Load More button

jQuery(function($){ // use jQuery code inside this to avoid "$ is not defined" error

var button = $(this),
data = {
'action': 'loadmore',
'query': wp_loadmore_params.posts, // that's how we get params from wp_localize_script() function
'page' : wp_loadmore_params.current_page

$.ajax({ // you can also use $.post here
url : wp_loadmore_params.ajaxurl, // AJAX handler
data : 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 posts

if ( wp_loadmore_params.current_page == wp_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

Step 4. wp_ajax_ #
This is the AJAX handler function. Insert it to your functions.php file.

function wp_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 here
query_posts( $args );

if( have_posts() ) :

// run the loop
while( 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 theme
get_template_part( 'template-parts/post/content', get_post_format() );
// for the test purposes comment the line above and uncomment the below one
// the_title();


die; // here we exit the script and even no wp_reset_query() required!

add_action('wp_ajax_loadmore', 'wp_loadmore_ajax_handler'); // wp_ajax_{action}
add_action('wp_ajax_nopriv_loadmore', 'wp_loadmore_ajax_handler'); // wp_ajax_nopriv_{action}

instagram volgers kopen volgers kopen buy windows 10 pro buy windows 11 pro