Skip to content

Instantly share code, notes, and snippets.

@tdmrhn
Created May 25, 2021 23:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdmrhn/d47f4444f7406cacf0f20c0473da7639 to your computer and use it in GitHub Desktop.
Save tdmrhn/d47f4444f7406cacf0f20c0473da7639 to your computer and use it in GitHub Desktop.
Blocksy Products live jQuery filter
<script>
jQuery(document).ready(function($) {
$('.ct-filter').on( 'click', function(event){
var $type = $(this).data("filter");
if($type == "all"){
$('.type-product').fadeOut(0);
$('.type-product').fadeIn(500);
} else {
$('.type-product').hide();
// For CPTs just change the category class to your CPTs slug for example: '.projects-'
$('.product_cat-' + $type + '.type-product').fadeIn(500);
}
});
});
</script>
<!-- For CPTs just change the category slug to your CPTs slug for example: 'projects' -->
<?php
$terms = get_terms(
array(
'taxonomy' => 'product_cat',
'hide_empty' => true,
)
);
if ( ! empty( $terms ) && is_array( $terms ) ) {
?>
<div class="ct-container" data-vertical-spacing="top">
<a data-filter="all" class="ct-button ct-filter">All</a>
<?php
foreach ( $terms as $term ) { ?>
<a data-filter="<?php echo $term->slug; ?>" class="ct-button ct-filter">
<?php echo $term->name; ?>
</a><?php
}
}
?>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment