Advertisement
xRomium

ajax filter

Apr 26th, 2022
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. NO changes in js filter
  2.  
  3. // functions.php
  4.  
  5.  
  6. function filter_projects() {
  7. $catSlug = $_POST['category'];
  8.  
  9.  
  10. $args = array(
  11. 'post_type' => 'project',
  12. 'tax_query' => array(
  13. array(
  14. 'taxonomy' => 'project-type',
  15. 'field' => 'slug',
  16. 'terms' => $catSlug,
  17. ),
  18. ),
  19. );
  20. $ajaxposts = new WP_Query( $args );
  21.  
  22. $response = '';
  23.  
  24. if($ajaxposts->have_posts()) {
  25. while($ajaxposts->have_posts()) : $ajaxposts->the_post();
  26. $response .= get_template_part('template-parts/project');
  27. endwhile;
  28. } else {
  29. $response = 'No projects';
  30. }
  31.  
  32. echo $response;
  33. exit;
  34. }
  35. add_action('wp_ajax_filter_projects', 'filter_projects');
  36. add_action('wp_ajax_nopriv_filter_projects', 'filter_projects');
  37.  
  38.  
  39.  
  40.  
  41.  
  42. // page-portfolio.php -- main content
  43.  
  44. <?php $categories = get_terms( [
  45. 'taxonomy' => 'project-type',
  46. 'hide_empty' => false,
  47. ] ); ?>
  48.  
  49. <div class="portfolio__filter">
  50. <li><a class="cat-list_item current-menu-item" href="/portfolio" data-slug="">All projects</a></li>
  51. <?php foreach($categories as $category) : ?>
  52. <li>
  53. <a class="cat-list_item" href="#!" data-slug="<?= $category->slug; ?>">
  54. <?= $category->name; ?>
  55. </a>
  56. </li>
  57. <?php endforeach; ?>
  58. </div>
  59.  
  60. <?php
  61. $projects = new WP_Query([
  62. 'post_type' => 'project',
  63. 'posts_per_page' => -1,
  64. 'order_by' => 'date',
  65. 'order' => 'desc',
  66. ]);
  67. ?>
  68.  
  69. <?php if($projects->have_posts()): ?>
  70. <div class="portfolio__content">
  71. <?php
  72. while($projects->have_posts()) : $projects->the_post();
  73. get_template_part('template-parts/project');
  74. endwhile;
  75. ?>
  76. </div>
  77. <?php wp_reset_postdata(); ?>
  78. <?php endif; ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement