ajax load more posts, wp_create_nonce, Votes Post, randon posts (ok)

Ví dụ 1: Không sử dụng wp_create_nonce

Bước 1: Trong file index.php
<?php
/**
 * The main template file
 *
 * This is the most generic template file in a WordPress theme
 * and one of the two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * E.g., it puts together the home page when no home.php file exists.
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 *
 * @package WordPress
 * @subpackage Twenty_Seventeen
 * @since 1.0
 * @version 1.0
 */

get_header(); ?>

<div class="wrap">
    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">
            <?php
            if ( have_posts() ) :
                while ( have_posts() ) : the_post();
                    get_template_part( 'template-parts/post/content', get_post_format() );
                endwhile;
            else :
                get_template_part( 'template-parts/post/content', 'none' );
            endif;
            ?>
        </main>
        <?php
            global $wp_query;
            if (  $wp_query->max_num_pages > 1 ) echo '<div class="misha_loadmore">More posts</div>';
        ?>
    </div>
</div>
<?php get_footer();
Bước 2:
// Ajax lionel
function misha_my_load_more_scripts() {
	global $wp_query;
	wp_enqueue_script('jquery');
	wp_register_script( 'my_loadmore', get_stylesheet_directory_uri() . '/assets/js/myloadmore.js', array('jquery') );
	wp_localize_script( 'my_loadmore', 'misha_loadmore_params', array(
			'ajaxurl' => admin_url('admin-ajax.php'),
			'posts' => json_encode($wp_query->query_vars),
			'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' );
function misha_loadmore_ajax_handler(){
	$args = json_decode( stripslashes( $_POST['query'] ), true );
	$args['paged'] = $_POST['page'] + 1;
	$args['post_status'] = 'publish';
	query_posts( $args );
	if( have_posts() ) :
		while( have_posts() ): the_post();
			get_template_part( 'template-parts/post/content', get_post_format() );
		endwhile;
	endif;
	die;
}
add_action('wp_ajax_loadmore', 'misha_loadmore_ajax_handler');
add_action('wp_ajax_nopriv_loadmore', 'misha_loadmore_ajax_handler');
Bước 3:
jQuery(document).ready(function($) {
	$('.misha_loadmore').click(function(){
		var button = $(this);
		var  data = {
			'action': 'loadmore',
			'query': misha_loadmore_params.posts,
			'page' : misha_loadmore_params.current_page
		};
		$.ajax({
			url : misha_loadmore_params.ajaxurl,
			data : data,
			type : 'POST',
			beforeSend : function ( xhr ) {
				button.text('Loading...');
			},
			success : function( data ){
				if( data ) { 
					button.text( 'More posts' ).prev().before(data);
					misha_loadmore_params.current_page++;
					if ( misha_loadmore_params.current_page == misha_loadmore_params.max_page ) 
						button.remove();
				} else {
					button.remove();
				}
			}
		});
	});
});

Ví dụ 2: Có sử dụng wp_create_nonce

Ví dụ 3: Một cách viết khác 3 (Dùng scroll)

Ví dụ 4: More ajax toan tap

Ví dụ 5:

Ví dụ 6: Votes Post

Ví dụ 6: randon posts

randon.php

Hướng dẫn của Demon Warlock

index.php

randon.php

Last updated

Was this helpful?