Hey dude: you are using a crap web browser. Upgrage to something better! We can rebuild your web experience. We have the technology. I recommend Chrome or Safari.


   acegoulet.com facebook  acegoulet.com twitter  acegoulet.com linkedin   [email protected]

Isotope and WordPress: How I Did It

If you’ve taken a look at the portfolio section of this site, you’ll notice I am using the Isotope jQuery plugin created by David DeSandro. I’ve been using this for a new section of the Parsons website that should be going live soon and figured it would work great for my site.

My main concern was not wanting to have to mess with the front end every time I want to update, so I started thinking how I could use WordPress to power Isotope. It turned out to be a lot easier than I had anticipated.

I created a category called “Portfolio” with several child categories (Design, Development, Photography). These child categories are pulled into the the portfolio page and the filter menu is built with the following code:

<ul id="filters" class="option-set filter" data-option-key="filter">
	<li>Filter: </li>
	<?php $categories = get_categories('orderby=name&depth=1&title_li=&use_desc_for_title=1&parent=82'); foreach ($categories as $cat) { ?>
	<li><a href="#"<?php if ($cat->slug == 'photos') {?> class="show-subnav filter-item" <?php } else {?>class="close-subnav filter-item" <?php }?>id="<?php echo $cat->slug; ?>" data-option-value=".<?php echo $cat->slug; ?>"><?= $cat->cat_name; ?></a></li>
	<?php } ?>
	<li><a href="#" data-option-value="*" id="showall" class="selected close-subnav filter-item">show all</a></li>
</ul>

So after the menu is generated, then the portfolio items need to added. This was a simple matter of creating posts, assigned to the child categories of “Portfolio”, using the built-it WordPress Featured Image for the thumbnails and a custom field for the link to the full size image. The titles of the items (captions for the fancybox) are pulled from the post content. If you are familiar with Isotope, you know that the filtering is mainly based on classes assigned to the items to be filtered. So I generate the items on the page with the following code, using the post categories to generate the necessary classes so Isotope can work its magic:

<div class="port port-file <?php $post_cats = get_the_category(); foreach( $post_cats as $category ) { echo $category->slug.' ';} ?>" data-category="portfolio">
	<a class="portfolio-img port-item-link showall <?php $post_cats = get_the_category(); foreach( $post_cats as $category ) { echo $category->slug.' ';} ?>" rel="portfolio" title="<?php the_content(); ?>" id="<?php echo get_post_meta($post->ID, 'trigger_id', true); ?>" href="<?php echo get_post_meta($post->ID, 'port_lg', true); ?>">
	<?php the_post_thumbnail(); ?></a>
</div>

And that’s pretty much it. Not all that complicated. If you happen upon this post and have any trouble implementing this yourself, feel free to leave a comment with any questions.

  • Sank

    hey there – great post :)

    trying to get this to work with custom post types and taxonomies…. any idea on how to get that to jive with the code you have here?
    thanks!

    • acegoulet

      Hi Sank,
      Great question. Not having done this yet with custom post types or taxonomies, I would imagine you would need the equivalent function to pull the array of terms from the custom taxonomy, which I believe is get_the_terms (http://codex.wordpress.org/Function_Reference/get_the_terms). You should be able to use that function to adapt the code I provided in the post. 

      • Sank

        thanks – yeah i looked into get_the_terms before posting here, but i think there’s a deeper issue i’m running into:

        the category filters are being output fine, it’s the thumbnails that are not being read… i’ve tried this on a site that doesn’t use custom post types and find the same result: no images.
        any advice?

        • acegoulet

          How are you trying to pull the image? It’s hard to figure out what’s going wrong without any details. I use the Featured Image tool in wordpress for each post, and then in the output use the “the_post_thumbnail();” output in the php.

          • Sank

            i used post_thumb as well pulling feat images… that’s why it’s baffling to me… should work…

            are there any variables that need to be changed in 2nd chunk of code? does that code need to be inside the loop? (i tried that as well but still nothing)

          • acegoulet

            Yes, the second chunk of code is inside the loop that generates the items in the isotope page. 

            Unfortunately, I don’t really have any other advice! Without looking at the code and looking at the wordpress admin side, it’s hard for me to know what’s going on.

          • Sank

            i’m willing to give you access if that is in the cards here :) would def love to see this working… thanks!

        • Cesmen

           Try:

          ID, ‘taxonomy’ ); ?>

          where ‘taxonomy’ is your custom taxonomy.

          • Cesmen

             wp_get_object_terms( $post->ID, ‘taxonomy’ )

  • Stace Felder

    Thanks for writing this up – it’s been really useful! But I’m getting a small bit of odd behavior and wondering if you might have an idea as to cause or a solution.  For some reason, when I first navigate to an Isotope page, or refresh that page, my images are all stacked on top of each other and the Isotope div is incorrectly sized. Once I filter or navigate away and back, everything is as it should be.

    You can see this at http://www.stacefelder.com/stacefelder/code

    Thanks for any pointers!

    • acegoulet

      Hi Stace - 

      It looks like isotope is running before the images are loaded and since the height of the isotope items is determined by the height of the images inside, isotope is calculating the height of the isotope items to be much smaller than the items actually should be. 
      Try adding height: 151px; to the css for your .port class (line 326 in your style.css file). Pretty sure that should do the trick.

      • Stace Felder

        No doubt!  I figured it might be a timing thing but I wasn’t sure how to correct for that. But setting the height on that div did the trick.  Seems so simple now!  Thanks very much for the help.

        • acegoulet

          No problem..Glad I could help!

          • Stace Felder

            Hate to come back on this but I gotta ask: setting that height to the fixed size causes issues with responsiveness.  When the page is viewed in a smaller window, the images adjust but that div stays the same. Any thoughts?

          • acegoulet

            Depends on how you are handling the responsiveness. If you were doing break-points, then you could use media queries to adjust the heights at each break-point. 

            Since you are using a 100% fluid grid, then you have two options that I can come up with off the top of my head right now. Either write a javascript that looks at the percentage width of the site (in relation to it’s largest size) and translates that into the height of the isotope items. OR write a bit of code that tells isotope to re-run after all the page elements have loaded. You would use the isotope “reLayout” function to do that.

          • Stace Felder

            I may end up going the break point route but trying fluid for now.  I’ve got the images to run before Isostope by going with ‘(window).load’ instead of ‘(document).ready’ but it seems slow – I can see my imgs load in a single vertical row before snapping to proper position. Your idea of pegging height to overall width is intriguing but, while I get it conceptually, I’m not sure how to write it.  If you have an idea, I’d be grateful. But you’ve already helped me a lot and we can call it a day.  Thanks very much!

          • acegoulet

            No problem! Glad I could be of any assistance.

  • Chris

    Where does the filter menu code go?
    Thanks for doing this.

    • acegoulet

      If I am understanding the question correctly, you are asking where to put the for the filters? That can go wherever you want that list of filters to appear on the page, just make sure it’s outside of the loop that generates your posts. On my portfolio page, I have my filter list above the filterable items…that’s a pretty standard placement.

  • jb

    Hi , thxs for the tut .. so 1st are u french? so it’d be easier to ask ..

    • acegoulet

      Hi JB,
      Sorry, I do not speak French, despite having a French name!

      • jb

        lol must not be easy for you everyday .. having a french name in the us (i guess) .. :)

  • Fuiba

    Hi!
    Thank you for the tutorial! But doesn’t work for me because i have infinite scroll. I used your exact settings of isotope (copied from ag-scripts.js) and works only for the top items of my portfolio.
    How can I solve this problem?
    Thanks!

    • acegoulet

      Hi Fuiba – Unfortunately, I have never used the infinite scroll feature of Isotope, so I can’t really speak to its functionality. Is your issue that the page isn’t bringing in more than just your top posts? Or is it just not filtering anything more than your top posts?