Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var page = 2;
- jQuery(function($) {
- jQuery('body').on('click', '.loadmore', function() {
- var data = {
- 'action': 'load_posts_by_ajax',
- 'page': page,
- 'security': blog.security
- };
- $.post(blog.ajaxurl, data, function(response) {
- if(response != '') {
- jQuery('.rowtest').append(response);
- page++;
- } else {
- jQuery('.loadmore').hide();
- }
- });
- });
- });
- /asets/css/customjs
- //function blog_scripts() {
- // Register the script
- wp_register_script( 'custom-script', get_stylesheet_directory_uri(). '/assets/css/custom.js', array('jquery'), false, true );
- // Localize the script with new data
- $script_data_array = array(
- 'ajaxurl' => admin_url( 'admin-ajax.php' ),
- 'security' => wp_create_nonce( 'load_more_posts' ),
- );
- wp_localize_script( 'custom-script', 'blog', $script_data_array );
- // Enqueued script with localized data.
- wp_enqueue_script( 'custom-script' );
- }
- add_action( 'wp_enqueue_scripts', 'blog_scripts' );
- function load_posts_by_ajax_callback() {
- check_ajax_referer('load_more_posts', 'security');
- $paged = $_POST['page'];
- $args = array(
- 'post_type' => 'reviews',
- 'post_status' => 'publish',
- 'posts_per_page' => '4',
- 'order'=>'ASC',
- 'paged' => $paged,
- );
- $my_posts = new WP_Query( $args );
- ?>
- <?php if ( $my_posts->have_posts() ) : ?>
- <?php while ( $my_posts->have_posts() ) : $my_posts->the_post(); ?>
- <div class="col-12 reviews" data-aos="zoom-in-up" data-aos-duration="1500">
- <div class="firstrw">
- <div class="foto-wrap">
- <?php the_post_thumbnail();?>
- <div class="firstrw_content">
- <h1><?php the_title();?></h1>
- <p id="data"><?php echo get_the_date( 'd-m-Y' ); ?></p>
- </div>
- </div>
- <div class="star-rating">
- <p><?php the_field('stars');?></p>
- </div>
- </div>
- <?php the_content();?>
- <hr>
- </div>
- <?php endwhile; ?>
- <?php
- endif;
- wp_die();
- }
- add_action('wp_ajax_load_posts_by_ajax', 'load_posts_by_ajax_callback');
- add_action('wp_ajax_nopriv_load_posts_by_ajax', 'load_posts_by_ajax_callback');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement