Advertisement
Guest User

Untitled

a guest
Oct 11th, 2021
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.80 KB | None | 0 0
  1. /*** The  filters ****/
  2.     <?php $categories = get_terms('roller'); ?>
  3.     <ul class="cat-list">
  4.       <li><a class="cat-list_item active" href="#!" data-slug="">All projects</a></li>
  5.  
  6.       <?php foreach($categories as $category) : ?>
  7.         <li>
  8.           <a class="cat-list_item" href="#!" data-slug="<?= $category->slug; ?>" data-type="upphovspersoner">
  9.             <?= $category->name; ?>
  10.           </a>
  11.         </li>
  12.       <?php endforeach; ?>
  13.     </ul>
  14.  
  15.  
  16.  /****** The script*******/
  17. <script>
  18. $('.cat-list_item').on('click', function(e) {
  19.     $('.cat-list_item').removeClass('active');
  20.     $(this).addClass('active');
  21.  
  22.     $.ajax({
  23.         type: 'POST',
  24.         url: '/wp-admin/admin-ajax.php',
  25.         dataType: 'html',
  26.         data: {
  27.           action: 'filter_projects',
  28.           taxonomy: $(this).data('slug'),
  29.         },
  30.         success: function(res) {
  31.           $('.project-tiles').html(res);
  32.         }
  33.     })
  34. });
  35. </script>
  36.  
  37. <?PHP
  38. /******* functoins.php *******/
  39. function filter_projects() {
  40.     $postType = $_POST['type'];
  41.     $catSlug = $_POST['taxonomy'];
  42.  
  43.     $projects = new WP_Query([
  44.        'post_type' => 'upphovspersoner',
  45.        'posts_per_page' => -1,
  46.        'tax_query' => [
  47.           [
  48.              'taxonomy' => 'roller',
  49.              'field'    => 'slug',
  50.              'operator' => 'IN'
  51.           ],
  52.        ]
  53.     ]);
  54.     $response = '';
  55.  
  56.     if($ajaxposts->have_posts()) {
  57.       while($ajaxposts->have_posts()) : $ajaxposts->the_post();
  58.         $response .= include('framework/upphovspersoner-item.php');
  59.       endwhile;
  60.     } else {
  61.       $response = 'empty';
  62.     }
  63.  
  64.     echo $response;
  65.     exit;
  66.   }
  67.   add_action('wp_ajax_filter_projects', 'filter_projects');
  68.   add_action('wp_ajax_nopriv_filter_projects', 'filter_projects');
  69. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement