azad_rpi

Categories list from queried post/post_type

Sep 11th, 2017
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.79 KB | None | 0 0
  1. $term_id_list = array();
  2.  
  3. $query = new WP_Query( $args );
  4.  
  5. // loop through every product
  6. while($query->have_posts()){
  7.     $query->the_post();
  8.  
  9.     // get categories of product
  10.     $product_terms = wp_get_post_terms( get_the_id(), 'product_cat');
  11.  
  12.     // check each term id with $term_id_list
  13.     // if this term id not in there, then push it to $term_id_list
  14.  
  15.     foreach($product_terms as $term){
  16.         if(!in_array($term->term_id, $term_id_list)){
  17.             $term_id_list[] = $term->term_id;
  18.         }
  19.     }
  20. }
  21.  
  22. // make portfolio filter using the category list
  23. <?php foreach($term_id_list as $term):
  24.     $term = get_term_by( 'id', $term, 'product_cat');
  25. ?>
  26.     <button data-filter=".product_cat-<?php echo $term->slug; ?>"><?php echo $term->name; ?></button>
  27. <?php endforeach; ?>
Advertisement
Add Comment
Please, Sign In to add comment