Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. jQuery( ".js-category, .js-date" ).on( "change", function() {
  2. var category = $( '.js-category' ).val();
  3. var date = $( '.js-date' ).val()
  4. data = {
  5. 'action': 'filterposts',
  6. 'category': category,
  7. 'date': date
  8. };
  9. $.ajax({
  10. url : ajaxurl,
  11. data : data,
  12. type : 'POST',
  13. beforeSend : function ( xhr ) {
  14. $('.filtered-posts').html( 'Laden...' );
  15. $('.js-category').attr( 'disabled', 'disabled' );
  16. $('.js-date').attr( 'disabled', 'disabled' );
  17. },
  18. success : function( data ) {
  19. if ( data ) {
  20. $('.filtered-posts').html( data.projecten );
  21.  
  22. $('.js-category').removeAttr('disabled');
  23. $('.js-date').removeAttr('disabled');
  24. } else {
  25. $('.filtered-posts').html( 'Helaas, er zijn geen projecten gevonden.' );
  26. }
  27. }
  28. });
  29. });
  30.  
  31. function ajax_filterposts_handler() {
  32. $category = esc_attr( $_POST['category'] );
  33. $date = esc_attr( $_POST['date'] );
  34.  
  35. $args = array(
  36. 'post_type' => 'projecten',
  37. 'post_status' => 'publish',
  38. 'posts_per_page' => -1,
  39. 'orderby' => 'date',
  40. 'order' => 'DESC'
  41. );
  42.  
  43. if ( $category != 'all' )
  44. $args['cat'] = $category;
  45.  
  46. if ( $date == 'new' ) {
  47. $args['order'] = 'DESC';
  48. } else {
  49. $args['order'] = 'ASC';
  50. }
  51.  
  52. $posts = 'Helaas, er zijn geen projecten gevonden.';
  53.  
  54. $the_query = new WP_Query( $args );
  55.  
  56. if ( $the_query->have_posts() ) :
  57. ob_start();
  58.  
  59. while ( $the_query->have_posts() ) : $the_query->the_post();
  60. get_template_part( 'includes/content-post' );
  61. endwhile;
  62.  
  63. $posts = ob_get_clean();
  64. endif;
  65.  
  66. $return = array(
  67. 'projecten' => $posts
  68. );
  69.  
  70. wp_send_json($return);
  71. }
  72. add_action( 'wp_ajax_filterposts', 'ajax_filterposts_handler' );
  73. add_action( 'wp_ajax_nopriv_filterposts', 'ajax_filterposts_handler' );
  74.  
  75. <div class="filter-wrap">
  76. <div class="category">
  77. <!-- <div class="field-title">Category</div> -->
  78. <?php $get_categories = get_categories(array('hide_empty' => 0)); ?>
  79. <select class="js-category">
  80. <option value="all">alles</option>
  81. <?php
  82. if ( $get_categories ) :
  83. foreach ( $get_categories as $cat ) :
  84. ?>
  85. <option value="<?php echo $cat->term_id; ?>">
  86. <?php echo $cat->name; ?>
  87. </option>
  88. <?php endforeach;
  89. endif;
  90. ?>
  91. </select>
  92. </div>
  93. </div>
  94.  
  95. <div class="filtered-posts">
  96. <?php
  97.  
  98. if ( have_posts() ) :
  99. while ( have_posts() ) : the_post();
  100. get_template_part( 'includes/content-post' );
  101. endwhile;
  102. endif;
  103.  
  104. ?>
  105. </div>
  106.  
  107. <article id="post-id-<?php the_id(); ?>" <?php post_class('clearfiks archive-projecten'); ?>>
  108. <a href="<?php the_permalink() ?>"><h3><?php the_title(); ?></h3>
  109. </article>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement