delvinkrasniqi

Untitled

Feb 5th, 2020
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. function blog_scripts() {
  2. // Register the script
  3. wp_register_script( 'custom-script', get_stylesheet_directory_uri(). '/js/custom.js', array('jquery'), false, true );
  4.  
  5. // Localize the script with new data
  6. $script_data_array = array(
  7. 'ajaxurl' => admin_url( 'admin-ajax.php' ),
  8. 'security' => wp_create_nonce( 'load_more_posts' ),
  9. );
  10. wp_localize_script( 'custom-script', 'blog', $script_data_array );
  11.  
  12. // Enqueued script with localized data.
  13. wp_enqueue_script( 'custom-script' );
  14. }
  15. add_action( 'wp_enqueue_scripts', 'blog_scripts' );
  16.  
  17.  
  18. function load_posts_by_ajax_callback() {
  19. check_ajax_referer('load_more_posts', 'security');
  20. $paged = $_POST['page'];
  21. $args = array(
  22. 'post_type' => 'reviews',
  23. 'post_status' => 'publish',
  24. 'posts_per_page' => '4',
  25. 'paged' => $paged,
  26. );
  27. $my_posts = new WP_Query( $args );
  28. ?>
  29.  
  30. <?php if ( $my_posts->have_posts() ) : ?>
  31. <?php while ( $my_posts->have_posts() ) : $my_posts->the_post(); ?>
  32. <div class="col-12 reviews">
  33. <div class="firstrw">
  34. <div class="foto-wrap">
  35. <?php the_post_thumbnail();?>
  36. <div class="firstrw_content">
  37. <h1><?php the_title();?></h1>
  38. <p id="data"><?php echo get_the_date( 'd-m-Y' ); ?></p>
  39. </div>
  40. </div>
  41. <div class="star-rating">
  42. <p><?php the_field('stars');?></p>
  43. </div>
  44. </div>
  45.  
  46. <?php the_content();?>
  47. <hr>
  48. </div>
  49. <?php endwhile; ?>
  50. <?php
  51. endif;
  52.  
  53. wp_die();
  54. }
  55. add_action('wp_ajax_load_posts_by_ajax', 'load_posts_by_ajax_callback');
  56. add_action('wp_ajax_nopriv_load_posts_by_ajax', 'load_posts_by_ajax_callback');
  57.  
  58.  
  59.  
  60. ///JS
  61. var page = 2;
  62. jQuery(function($) {
  63. $('body').on('click', '.loadmore', function() {
  64. var data = {
  65. 'action': 'load_posts_by_ajax',
  66. 'page': page,
  67. 'security': blog.security
  68. };
  69.  
  70. $.post(blog.ajaxurl, data, function(response) {
  71. if(response != '') {
  72. $('.reviews').append(response);
  73. page++;
  74. } else {
  75. $('.loadmore').hide();
  76. }
  77. });
  78. });
  79. });
  80. ////HTML
  81.  
  82.  
  83. <div class="container">
  84. <div class="row">
  85. <?php
  86. $args = array(
  87. 'post_type' => 'reviews',
  88. 'post_status' => 'publish',
  89. 'posts_per_page' => '3',
  90. 'paged' => 1,
  91. );
  92. $loop = new WP_Query($args);
  93. while ($loop->have_posts()) :
  94. $loop->the_post();
  95. ?>
  96.  
  97. <div class="col-12 reviews">
  98. <div class="firstrw">
  99. <div class="foto-wrap">
  100. <?php the_post_thumbnail();?>
  101. <div class="firstrw_content">
  102. <h1><?php the_title();?></h1>
  103. <p id="data"><?php echo get_the_date( 'd-m-Y' ); ?></p>
  104. </div>
  105. </div>
  106. <div class="star-rating">
  107. <p><?php the_field('stars');?></p>
  108. </div>
  109. </div>
  110.  
  111. <?php the_content();?>
  112. <hr>
  113. </div>
  114.  
  115.  
  116.  
  117. <?php
  118. endwhile;
  119. wp_reset_postdata();
  120. ?>
  121. <div class="loadmore">Load More...</div>
  122. </div>
  123. </div>
  124. </div>
  125. </div>
Advertisement
Add Comment
Please, Sign In to add comment