Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. 1) functions.php
  2. function kodeks_scripts() {
  3. if ( ! is_admin() ) {
  4. //custom javascript
  5. wp_enqueue_script( 'jquery' );
  6. wp_enqueue_script( 'app-js', get_template_directory_uri() . '/js/app.min.js', null, null, true );
  7.  
  8. wp_localize_script( 'app-js', 'myajax', array(
  9. 'ajax_url' => admin_url( 'admin-ajax.php' ),
  10. 'is_user_logged_in' => is_user_logged_in()
  11. ) );
  12.  
  13. }
  14. }
  15. add_action( 'wp_enqueue_scripts', 'kodeks_scripts' );
  16.  
  17. 2) php file
  18. <?php $post_per_page = 4; ?>
  19. <?php $arg = array(
  20. 'post_type' => 'clients_cpt',
  21. 'order' => 'DESC',
  22. 'orderby' => 'date',
  23. 'posts_per_page' => $post_per_page
  24. ); ?>
  25.  
  26. <?php $the_query = new WP_Query( $arg );
  27. if ( $the_query->have_posts() ) : ?>
  28. <section class="clients post-type">
  29. <div class="clients-container grid-container grid-container--full"
  30. data-per-page="<?php echo $post_per_page; ?>"
  31. data-clients="<?php echo wp_count_posts( 'clients_cpt' )->publish; ?>">
  32.  
  33. <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
  34.  
  35. <?php show_template( 'clients-item', null, 'template-parts/cpt' ); ?>
  36.  
  37. <?php endwhile; ?>
  38. </div>
  39. <div class="grid-container grid-container--full">
  40. <div class="grid-x">
  41. <div class="cell text-center">
  42. <a href="#"
  43. class="js-button button button--rounded"><?php _e( 'LAST FLERE JOBBER', 'foundation' ); ?></a>
  44. </div>
  45. </div>
  46. </div>
  47. </section>
  48. <?php endif;
  49. wp_reset_query(); ?>
  50.  
  51. //clients-item.php part
  52. <div class="clients-row post-type__row grid-x align-middle">
  53. <div class="post-type__col clients-left-col cell large-6">
  54. <div class="post-type__img" <?php bg( get_the_post_thumbnail_url(), 'large' ); ?>></div>
  55. </div>
  56. <div class="post-type__col clients-right-col cell large-6">
  57. <a href="<?php the_permalink(); ?>" class="post-link">
  58. <div class="post">
  59. <div class="post__inner">
  60. <?php global $post; ?>
  61. <?php if ( $view_subtitle = get_sub_field( 'view_subtitle' ) ): ?>
  62. <div class="subtitle subtitle--gray"><?php echo $view_subtitle; ?></div>
  63. <?php elseif ( ( $post_terms = wp_get_object_terms( $post->ID, get_object_taxonomies( $post->post_type ) ) ) ) : ?>
  64. <div class="subtitle subtitle--gray"><?php echo $post_terms[0]->name; ?></div>
  65. <?php endif; ?>
  66. <h2 class="post__title"><?php the_title(); ?></h2>
  67. <div class="post__excerpt"><?php the_excerpt(); ?></div>
  68. <span class="arrow icon-arrow-right1"></span>
  69. </div>
  70. </div>
  71. </a>
  72. </div>
  73. </div>
  74.  
  75.  
  76. 3) app.js
  77.  
  78. //Ajax load more posts
  79. var loaded = false;
  80. $('.js-button').click(function (e) {
  81. e.preventDefault();
  82. if (!loaded) {
  83. loaded = true;
  84. var clientButton = $(this);
  85. var container = clientButton.closest('.clients').find('.clients-container');
  86. var clientsCount = container.data('clients');
  87. var clientsPerPage = container.data('per-page');
  88. var clientsLoaded = container.find('.clients-row').length;
  89. $.post(myajax.ajax_url, {
  90. action: 'load_more',
  91. offset: clientsLoaded,
  92. perpage: clientsPerPage
  93. }).success(function (response) {
  94. container.append(response);
  95. loaded = false;
  96. if (clientsCount <= clientsLoaded + clientsPerPage) {
  97. clientButton.hide();
  98. }
  99. })
  100. }
  101. });
  102.  
  103. 4) functions.php
  104.  
  105.  
  106. //Ajax load more posts
  107.  
  108. add_action( 'wp_ajax_load_more', 'load_more_callback' );
  109. add_action( 'wp_ajax_nopriv_load_more', 'load_more_callback' );
  110.  
  111. function load_more_callback() {
  112. $news = new WP_Query( array(
  113. 'post_type' => 'clients_cpt',
  114. 'order' => 'DESC',
  115. 'orderby' => 'date',
  116. 'posts_per_page' => $_POST['perpage'],
  117. 'offset' => $_POST['offset']
  118. ) );
  119. if ( $news->have_posts() ) {
  120. while ( $news->have_posts() ) {
  121. $news->the_post();
  122. show_template( 'clients-item', null, 'template-parts/cpt' );
  123. }
  124. }
  125. wp_die();
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement