Advertisement
DizaBlah

Dez Gallery

Mar 27th, 2023
901
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.67 KB | Source Code | 0 0
  1. <?php
  2. // Get all categories for the gallery
  3. $categories = get_terms('dez_gallery_category', array(
  4.     'hide_empty' => false,
  5. ));
  6.  
  7. // Get all industries for the gallery
  8. $industries = get_terms('dez_gallery_industry', array(
  9.     'hide_empty' => false,
  10. ));
  11. ?>
  12.  
  13. <form class="dez-gallery-filters">
  14.     <label>
  15.         <?php esc_html_e('Categories:', 'text-domain'); ?><br>
  16.         <?php foreach ($categories as $category) : ?>
  17.             <input type="checkbox" name="dez-gallery-category[]" value="<?php echo esc_attr($category->term_id); ?>"> <?php echo esc_html($category->name); ?><br>
  18.         <?php endforeach; ?>
  19.     </label>
  20.  
  21.     <label>
  22.         <?php esc_html_e('Industries:', 'text-domain'); ?><br>
  23.         <?php foreach ($industries as $industry) : ?>
  24.             <input type="checkbox" name="dez-gallery-industry[]" value="<?php echo esc_attr($industry->term_id); ?>"> <?php echo esc_html($industry->name); ?><br>
  25.         <?php endforeach; ?>
  26.     </label>
  27.  
  28.     <button type="submit"><?php esc_html_e('Filter', 'text-domain'); ?></button>
  29. </form>
  30.  
  31.  
  32. ////<?php
  33. // Get the selected category and industry filters
  34. $category_filters = isset($_GET['dez-gallery-category']) ? array_map('intval', $_GET['dez-gallery-category']) : array();
  35. $industry_filters = isset($_GET['dez-gallery-industry']) ? array_map('intval', $_GET['dez-gallery-industry']) : array();
  36.  
  37. // Setup gallery query arguments
  38. $gallery_args = array(
  39.     'post_type' => 'dez_gallery',
  40.     'posts_per_page' => -1,
  41. );
  42.  
  43. // Add category filters to query
  44. if (!empty($category_filters)) {
  45.     $gallery_args['tax_query'][] = array(
  46.         'taxonomy' => 'dez_gallery_category',
  47.         'field' => 'term_id',
  48.         'terms' => $category_filters,
  49.         'operator' => 'IN',
  50.     );
  51. }
  52.  
  53. // Add industry filters to query
  54. if (!empty($industry_filters)) {
  55.     $gallery_args['tax_query'][] = array(
  56.         'taxonomy' => 'dez_gallery_industry',
  57.         'field' => 'term_id',
  58.         'terms' => $industry_filters,
  59.         'operator' => 'IN',
  60.     );
  61. }
  62.  
  63. // Get gallery items
  64. $gallery_query = new WP_Query($gallery_args);
  65.  
  66. // Display gallery items
  67. if ($gallery_query->have_posts()) :
  68.     ?>
  69.     <div class="dez-gallery-items">
  70.         <?php while ($gallery_query->have_posts()) : $gallery_query->the_post(); ?>
  71.             <div class="dez-gallery-item">
  72.                 <?php if (has_post_thumbnail()) : ?>
  73.                     <a href="<?php the_permalink(); ?>">
  74.                         <?php the_post_thumbnail('medium'); ?>
  75.                     </a>
  76.                 <?php endif; ?>
  77.             </div>
  78.         <?php endwhile; ?>
  79.     </div>
  80.     <?php
  81. endif;
  82.  
  83. // Reset post data
  84. wp_reset_postdata();
  85. ?>
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement