2

I'm trying to filter from an entirely different page, to where my Isotope is.

Currently, I am calling isotope sucessfully with:

 $(function() {
    // cache container
    var $container = $('.isotope-container');
    var $defaultfilter = $('.feature-this');
    $('.isotope-container').isotope({
      filter: '.feature-this',
      masonry: { columnWidth: 326, resizesContainer: false }
    });

    // filter items when filter link is clicked
    $('#filters a').click(function(){
      var selector = $(this).attr('data-filter');
      $container.isotope({ filter: selector });
      return false;
    });
    // set selected menu items
   var $optionSets = $('.option-set'),
       $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('.option-set');
   $optionSet.find('.selected').removeClass('selected');
   $this.addClass('selected'); 
  });
});

I would like to, from another page, filter results when a link is clicked, with a markup like:

    <ul>
        <li><a href="/isotope/#filter=.filter1">Filter 1</a></li>
        <li><a href="/isotope/#filter=.filter2">Filter 2</a></li>
        <li><a href="/isotope/#filter=.filter3">Filter 3</a></li>
        <li><a href="/isotope/#filter=.filter4">Filter 4</a></li>
        <li><a href="/isotope/#filter=.filter5">Filter 5</a></li>
    </ul>

I can't get it to work. It works as desired here, but not from a different page:

http://isotope.metafizzy.co/demos/hash-history.html

SMacFadyen
  • 3,145
  • 2
  • 25
  • 37

1 Answers1

2

I've got just the answer for you - I wrote a blog post about it;

Isotope Filtering from different Wordpress Page

Just for link rot etc I'll copy it out here. I used the jQuery cookie plugin and created and destroyed a cookie which passed over the filter to the page containing Isotope. In my case there was a permanent main menu of filters on each page in header.php. However, I used this code to give the menu a different class depending on whether the user was on the home page (which had the Isotope on it) or another page;

<?php

if (is_front_page()) {
   echo '<ul id="filters">';
} else {
   echo '<ul id="cookiefilter">';
}

?>

Then the Javascript was changed so that if the link being clicked was part of cookiefilter menu (ie off the Isotope page) then the #cookiefilter a click event was fired and a cookie created. The code block below that filters Isotope based on the cookie and then destroys it. The site it's on is;

http://www.face-educationmarketing.co.uk

To see it in action an explanation. The home page has Isotope on it and all the entries underneath the 'Work' header are Isotope filters. The 'About' header is a WP menu, click on one of those links under 'About' and you are taken to different WP page. On that page you would examine the change in the markup on the Work filter list - the ul class has changed, it is now;

<ul class="cookiefilter">

rather than;

<ul class="filters">

This is the important bit because in the JS below, a click event on #filters a behaves differently to click events on #cookiefilter a

In the latter case a cookie is created with the filter name in it. The onload JS checks to see if such a cookie exists, if it does then it calls Isotope, filters it with the value in the cookie and then destroys the cookie so the normal filters then work. Here's the JS;

    // filter items when filter link is clicked
    $('#filters a').click(function(){
        var selector = $(this).attr('data-filter');
        $container.isotope({ filter: selector });
        return false;
    });

    // Set cookie based on filter selector
    $('#cookiefilter a').click(function(){
        var selector = $(this).attr('data-filter');
        $.cookie("listfilter", selector, { path: '/' });
    });

    if ( $.cookie("listfilter") ) {
        $container.isotope({ filter: $.cookie("listfilter") });
        $.cookie("listfilter", null, { path: '/' });
        return false;
    }

Adding an 'Active' class to the link

Some more info based on the comments. A good question, how would you add a class to the active filter so that you can apply CSS to show the user that filter is active?

I've done that on my own homepage (http://www.mcnab.co) using the following JS. Have a look at the Magento, Wordpress, Joomla section on the right hand side of the masthead. Those are Isotope filters.

This script clears all classes from the links contained in the unordered list with id filters when a link is pressed and adds the class 'active' to the clicked link before refiring Isotope;

    var $filterlist = $('ul#filters');
    // filter items when filter link is clicked
    $('#filters a').click(function(){
        $this = $(this);

        var selector = $(this).attr('data-filter');

        // Remove classes from all filters
        $filterlist.find('a').each(function(){
            $(this).removeClass();
        }); 

        // add active class to clicked filter
        $this.addClass('active');

        // refire isotope using the newly clicked filter
        $container.isotope({ filter: selector });
        return false;
    });

To do this from another page you would add a similar script to the #cookiefilter a which found the link with the date-filter attribute with the same as the cookie and add the 'active' class to that.

McNab
  • 6,767
  • 4
  • 35
  • 55
  • Hello mate, would it be possible to see the markup of your page? I actually found your blog post but couldn't get it to work - it felt slightly rushed/ill explained for someone who isnt familiar with jQuery. Your face demo isn't working for me right now. – SMacFadyen Sep 26 '12 at 15:50
  • 1
    OK, I've fleshed it out a bit with explanations. All the markup is there on that Face demo page as it's the JS and html that you need, there's no real PHP other than the class changer. The example is definitely working BTW, I just checked it in a panic, explanation on how to see it in the edited answer now. Hope this helps more, I'll update the blog post if it's not clear (mine are all rushed!!) – McNab Sep 26 '12 at 16:02
  • Thanks mate, I appreciate the time you've taken to do that. I will accept this answer as soon as I get to take a look at it in regards to my build. :) – SMacFadyen Sep 27 '12 at 09:25
  • I've actually got this to work, but just having a issue with setting 'selected' class to my , and not sure how you achieved it to 'shuffle' as mine doesn't do the animation.. – SMacFadyen Sep 27 '12 at 16:18
  • Updated the answer to include a section on adding an active/selected class. Not sure about the animation - that's just the default, I didn't do anything fancy there. – McNab Sep 27 '12 at 19:23
  • @McNab thanks for this great solution btw, exactly what I was after. I'm having one small problem though that you might be able to help me with...I've just included a small portion of the jQuery in a fiddle to show you my isotope markup: http://jsfiddle.net/p6Rex/ when navigating to the `whats-on` page via a `.slide-menu-hash a` click function, the isotope works great and filters the relevant divs as it should. BUT when navigating to the `whats-on` page via the url, ALL divs are being filtering out and I'm greeted with a blank page, just wondering if you might have a solution to this? – user1374796 Mar 09 '14 at 18:37