Skip to content

Instantly share code, notes, and snippets.

@infocities
Forked from der-lukas/cpt_loop.php
Created November 6, 2021 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save infocities/d26b71c83c2470ced44ea4991a1bb782 to your computer and use it in GitHub Desktop.
Save infocities/d26b71c83c2470ced44ea4991a1bb782 to your computer and use it in GitHub Desktop.
Isotope + Filtering with Custom Taxonomy and Custom Post Type
<!-- PROJECTS -->
<section id="projects" class="row">
<?php
$args = array(
'post_type' => 'pt-project',
'posts_per_page' => -1,
'orderby' => 'menu_order', // When order via "Simple Page Ordering"-Plugin
'order' => 'ASC'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$terms = get_the_terms( $post->ID, 'kategorie' ); // Custom Taxonomy Terms
if ( $terms && ! is_wp_error( $terms ) ) :
$links = array();
foreach ( $terms as $term ) {
$links[] = $term->name;
}
$tax_links = join( " ", str_replace(' ', '-', $links));
$tax = strtolower($tax_links);
else :
$tax = '';
endif;
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id(), 'medium');
$url = $thumb[0];
$width = $thumb[1];
$height = $thumb[2];
?>
<article class="item project-container four <?php echo $tax; ?>">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" class="project-inner">
<div class="project-info">
<div class="project-info-inner">
<div class="info-content">
<span class="project-icon icon-<?php echo $tax; ?>"><?php echo $tax_links; ?></span>
<h1><?php the_title(); ?></h1>
</div>
</div>
</div>
<img src="<?php echo $url; ?>" width="<?php echo $width; ?>" height="<?php echo $height; ?>" />
</a>
</article>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</section>
<!-- PROJECTS END -->
<!-- PROJECT FILTER -->
<div class="filters">
<?php
/* $args = array('hide_empty' => false); */
$terms = get_terms('kategorie', $args); // Get Taxonomy Terms
$count = count($terms);
echo '<div class="filter"><a href="#filter=*" title="" class="active"><span>Alle Projekte</span></a></div>';
if ( $count > 0 ){
foreach ( $terms as $term ) {
$termname = strtolower($term->name);
$termname = str_replace(' ', '-', $termname);
echo '<div class="filter ' . $termname . '"><a href="#filter=.'.$termname.'" title="" data-filter=".'.$termname.'"><span>'.$term->name.'</span></a></div>';
}
}
?>
</div>
<!-- PROJECT FILTER END -->
/* Isotope Transitions
------------------------------- */
.isotope,
.isotope .item {
-webkit-transition-duration: 0.8s;
transition-duration: 0.8s;
}
.isotope {
-webkit-transition-property: height, width;
transition-property: height, width;
}
.isotope .item {
-webkit-transition-property: -webkit-transform, opacity;
transition-property: transform, opacity;
}
/* responsive media queries */
@media (max-width: 768px) {
header h1 small {
display: block;
}
header div.description {
padding-top: 9px;
padding-bottom: 4px;
}
.isotope .item {
position: static ! important;
-webkit-transform: translate(0px, 0px) ! important;
-ms-transform: translate(0px, 0px) ! important;
transform: translate(0px, 0px) ! important;
}
}
function getHashFilter() {
var hash = location.hash;
// get filter=filterName
var matches = location.hash.match( /filter=([^&]+)/i );
var hashFilter = matches && matches[1];
return hashFilter && decodeURIComponent( hashFilter );
}
var $container = $('#projects');
var $filters = $('.filters');
var isIsotopeInit = false;
var $item = $(".project-container");
// bind filter button click
/*
var $filters = $('.filters').on( 'click', 'a', function(e) {
e.preventDefault();
var filterAttr = $( this ).attr('data-filter');
location.hash = 'filter=' + encodeURIComponent( filterAttr );
});
*/
function onHashchange() {
var hashFilter = getHashFilter();
if ( !hashFilter && isIsotopeInit ) {
return;
}
if(!hashFilter) {
$container.isotope({
itemSelector: '.item',
filter: '*'
});
$filters.find('a[href="#filter=*"]').addClass('active');
}
isIsotopeInit = true;
// filter isotope
$container.isotope({
itemSelector: '.item',
filter: hashFilter
});
// set selected class on button
if ( hashFilter ) {
$filters.find('.active').removeClass('active');
$filters.find('a[href="#filter=' + hashFilter + '"]').addClass('active');
}
$(window).resize();
}
$(window).on( 'hashchange', onHashchange );
$('.main').imagesLoaded().always( function( instance ) {
onHashchange();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment