Reputation: 21
I use Isotope on my Wordpress Website to filter my custom post. I have a CPT Film (production
) and multiple taxonomies : type
(Animation / Horror...), years
(2012 / 2013) and so on.
For the moment I can sort by years and every things work fine with this code in my template page:
<div class="row">
<ul id="filters">
<li>Selectionner</li>
<li><a href="#" data-filter="*" class="selected">Toutes les années</a></li>
<?php
$terms = get_terms("annee"); // get all categories, but you can use any taxonomy
$count = count($terms); //How many are they?
if ( $count > 0 ){ //If there are more than 0 terms
foreach ( $terms as $term ) { //for each term:
echo "<li><a href='#' data-filter='.".$term->slug."'>" . $term->name . "</a></li>\n";
//create a list item with the current term slug for sorting, and name for label
}
}
?>
</ul>
After the list, the loop that show all post and sort by years:
<?php
$the_query = new WP_Query( array(
'posts_per_page' => 16,
'post_type' => array( 'production' )
)); //Check the WP_Query docs to see how you can limit which posts to display ?>
<?php if ( $the_query->have_posts() ) : ?>
<div id="isotope-list">
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
$termsArray = get_the_terms( $post->ID, "annee" ); //Get the terms for this particular item
$termsString = ""; //initialize the string that will contain the terms
foreach ( $termsArray as $term ) { // for each term
$termsString .= $term->slug.' '; //create a string that has all the slugs
}
?>
<div class="<?php echo $termsString; ?> item three columns gallery grid"> <?php // 'item' is used as an identifier (see Setp 5, line 6) ?>
<figure class="effect-ming">
<?php the_post_thumbnail('miniature-film'); ?>
<figcaption>
<h2><?php the_title('') ?></h2>
<p class="serif"><?php the_field('realisation'); ?><p>
</figcaption>
</figure>
</div> <!-- end item -->
<?php endwhile; ?>
<?php else : ?>
<p>Aucun résultat ne correspond à votre recherche </p>
<a href="<?php bloginfo('url'); ?>/catalogue">Retour au catalogue</a>
</div> <!-- end isotope-list -->
<?php endif; ?>
</div>
And the following javascript:
jQuery(function ($) {
var $container = $('#isotope-list'); //The ID for the list with all the blog posts
$container.isotope({ //Isotope options, 'item' matches the class in the PHP
itemSelector : '.item',
layoutMode : 'masonry'
});
//Add the class selected to the item that is clicked, and remove from the others
var $optionSets = $('#filters '),
$optionLinks = $optionSets.find('a');
$optionLinks.click(function(){
var $this = $(this);
// don't proceed if already selected
if ( $this.hasClass('selected') ) {
return false;
}
var $optionSet = $this.parents('#filters');
$optionSets.find('.selected').removeClass('selected');
$this.addClass('selected');
//When an item is clicked, sort the items.
var selector = $(this).attr('data-filter');
$container.isotope({ filter: selector });
return false;
});
});
(function( $ ) {
$( function() {
var $container = $('.isotope').isotope({
itemSelector: '.element-item',
layoutMode: 'fitRows'
});
$('#filters').on( 'click', 'button', function() {
var filterValue = $( this ).attr('data-filter');
$container.isotope({ filter: filterValue });
});
});
})(jQuery);
But I want to make multiple filters like years and use them at the same time, but I don't know how to do that.
I have tried to duplicate the code I used for years
and change the slug by "type"
but it didn't worked.
Here is an example that show you what I want to do: http://codepen.io/desandro/pen/GFbAs
It iss made with isotope, not with Wordpress functions, so I don't know how to change it.
Upvotes: 1
Views: 1111
Reputation: 1705
First, add a new custom data parameter to your links, such as data-filter-two=""
Then add an additional jQuery click function to handle that filtering -
$('#filters').on( 'click', 'button', function() {
var filterValue = $( this ).attr('data-filter-two');
$container.isotope({ filter: filterValue });
});
Upvotes: 1