Advertisement
delvinkrasniqi

Untitled

Mar 13th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. var page = 2;
  2. jQuery(function($) {
  3. jQuery('body').on('click', '.loadmore', function() {
  4. var data = {
  5. 'action': 'load_posts_by_ajax',
  6. 'page': page,
  7. 'security': blog.security
  8. };
  9.  
  10. $.post(blog.ajaxurl, data, function(response) {
  11. if(response != '') {
  12. jQuery('.rowtest').append(response);
  13. page++;
  14. } else {
  15. jQuery('.loadmore').hide();
  16. }
  17. });
  18. });
  19. });
  20.  
  21. /asets/css/customjs
  22.  
  23. //function blog_scripts() {
  24. // Register the script
  25. wp_register_script( 'custom-script', get_stylesheet_directory_uri(). '/assets/css/custom.js', array('jquery'), false, true );
  26.  
  27. // Localize the script with new data
  28. $script_data_array = array(
  29. 'ajaxurl' => admin_url( 'admin-ajax.php' ),
  30. 'security' => wp_create_nonce( 'load_more_posts' ),
  31. );
  32. wp_localize_script( 'custom-script', 'blog', $script_data_array );
  33.  
  34. // Enqueued script with localized data.
  35. wp_enqueue_script( 'custom-script' );
  36. }
  37. add_action( 'wp_enqueue_scripts', 'blog_scripts' );
  38.  
  39.  
  40. function load_posts_by_ajax_callback() {
  41. check_ajax_referer('load_more_posts', 'security');
  42. $paged = $_POST['page'];
  43. $args = array(
  44. 'post_type' => 'reviews',
  45. 'post_status' => 'publish',
  46. 'posts_per_page' => '4',
  47. 'order'=>'ASC',
  48. 'paged' => $paged,
  49. );
  50. $my_posts = new WP_Query( $args );
  51. ?>
  52.  
  53. <?php if ( $my_posts->have_posts() ) : ?>
  54. <?php while ( $my_posts->have_posts() ) : $my_posts->the_post(); ?>
  55. <div class="col-12 reviews" data-aos="zoom-in-up" data-aos-duration="1500">
  56. <div class="firstrw">
  57. <div class="foto-wrap">
  58. <?php the_post_thumbnail();?>
  59. <div class="firstrw_content">
  60. <h1><?php the_title();?></h1>
  61. <p id="data"><?php echo get_the_date( 'd-m-Y' ); ?></p>
  62. </div>
  63. </div>
  64. <div class="star-rating">
  65. <p><?php the_field('stars');?></p>
  66. </div>
  67. </div>
  68.  
  69. <?php the_content();?>
  70. <hr>
  71. </div>
  72. <?php endwhile; ?>
  73. <?php
  74. endif;
  75.  
  76. wp_die();
  77. }
  78. add_action('wp_ajax_load_posts_by_ajax', 'load_posts_by_ajax_callback');
  79. add_action('wp_ajax_nopriv_load_posts_by_ajax', 'load_posts_by_ajax_callback');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement