Advertisement
martawijaya

Load More Ajax

May 8th, 2019
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.11 KB | None | 0 0
  1. function blog_post_item() {
  2.   ob_start();
  3.   ?>
  4.  
  5. $list_post = array(
  6.     'post_type' => 'post',
  7.     'posts_per_page' => 6,
  8.     'post_status' => 'publish',
  9.     'order_by' => 'slug',
  10.     'order' => 'DESC',
  11.   );
  12.  
  13.   $list_query = new WP_Query( $list_post );
  14.   ?>
  15.   <div id="blog-all-item" class="row">
  16.   <?php
  17.   if( $list_query->have_posts() ) {
  18.     while( $list_query->have_posts() ) {
  19.       $list_query->the_post();
  20.       $num = $list_query->post_count;
  21.       $thumb2 = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' ); ?>
  22.       <div class="col-xl-4 col-lg-4 col-md-4 col-sm-12 col-xs-12 blog-item-wrap">
  23.           <a class="post-list-item" href="<?php echo get_permalink(); ?>">
  24.               <div class="post-list-item-wrapper">
  25.                 <div class="item-post-thumbnail">
  26.                    <img class="lazyload" data-src="<?php echo $thumb2[0]; ?>">
  27.                 </div>
  28.                 <div class="item-post-content">
  29.                     <h2><?php echo the_title(); ?></h2>
  30.                     <span class="post-date"><?php the_time('j F Y'); ?></span>
  31.                     <p><?php echo wp_trim_words( get_field('introduce_text'), 15 );?></p>
  32.                 </div>
  33.               </div>
  34.            </a>
  35.       </div>
  36.                
  37.   <?php
  38.     }
  39.   }
  40.   ?>
  41.   </div>
  42.   <?php
  43.     $total = wp_count_posts()->publish;
  44.   ?>
  45.   <div class="center-btn">
  46.     <?php
  47.        if ($total > 6) {
  48.         ?><div id="more_posts">Load More</div><?php
  49.        }
  50.     ?>
  51.   </div>
  52.   <?php
  53.   return ob_get_clean();
  54. }
  55.  
  56.  
  57. add_shortcode('blog-post-item', 'blog_post_item');
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65. function more_post_ajax(){
  66.  
  67.  
  68.     $ppp = (isset($_POST["ppp"])) ? $_POST["ppp"] : 6;
  69.     $page = (isset($_POST['pageNumber'])) ? $_POST['pageNumber'] : 0;
  70.  
  71.     header("Content-Type: text/html");
  72.  
  73.     $args = array(
  74.         'suppress_filters' => true,
  75.         'post_type' => 'post',
  76.         'posts_per_page' => $ppp,
  77.         'paged'    => $page,
  78.     );
  79.  
  80.     $loop = new WP_Query($args);
  81.  
  82.     $out = '';
  83.  
  84.     if ($loop -> have_posts()) :  while ($loop -> have_posts()) : $loop -> the_post();
  85.  
  86.          $thumb2 = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
  87.          $out .= '<div class="col-xl-4 col-lg-4 col-md-4 col-sm-12 col-xs-12 blog-item-wrap">
  88.              <a class="post-list-item" href="'.get_the_permalink().'">
  89.                  <div class="post-list-item-wrapper">
  90.                    <div class="item-post-thumbnail">
  91.                       <img class="lazyload" data-src="'.$thumb2[0]. '">
  92.                    </div>
  93.                    <div class="item-post-content">
  94.                        <h2>'.get_the_title().'</h2>
  95.                        <span class="post-date">'.get_the_time('j F Y').'</span>
  96.                        <p>'.wp_trim_words( get_field('introduce_text'), 15 ).'</p>
  97.                    </div>
  98.                  </div>
  99.               </a>
  100.          </div>';
  101.  
  102.     endwhile;
  103.     endif;
  104.     wp_reset_postdata();
  105.     die($out);
  106. }
  107.  
  108. add_action('wp_ajax_nopriv_more_post_ajax', 'more_post_ajax');
  109.   add_action('wp_ajax_more_post_ajax', 'more_post_ajax');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement