0

I am currently building a wordpress theme using the isotope.js script which allows you to display articles on the same step by filtering their categories, I struggled a lot and I still can't get the expected result:

Here a link which will lead you to the expected result (a full version in HTML / CSS):

https://vgamerz.fr/isotopehelp/gallery/blog.html

Here my wordpress theme (PHP Files):

    <!-- Isotope Header -->
      <div class="row">
        <div class="col-md-12">

            <h2 class="isotope-button-title">Isotope Blog Sort By</h2>
                <div id="isotope-filters" class="isotope-button-group">

                        <li><a href="#" data-filter="*" class="selected">Everything</a></li>
                        <?php 
                        $terms = get_categories(); // 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 "<button class='button'><a href='#' data-filter='.".$term->slug."'>" . $term->name . "</a></button>\n";
                            //create a list item with the current term slug for sorting, and name for label
                            }
                        } 
                            ?>

                </div>

          </div>
        </div>
        <!-- Isotope Header -->

    <!--------------------------------------- end of header -------------------------------------->

        <!-- Isotope Grid -->
        <div class="row isotope-grid">
            <?php
                // Loops
                if(have_posts()):
                    while(have_posts()):
                        the_post(); ?>    
                                <!-- Isotope Grid-Item -->
                                <div class="col-md-3 col-sm-4 col-xs-12 isotope-grid-item  design " data-category="<?php get_the_category($post->ID); ?>">
                                    <div class="isotope-item">
                                        <a href="#"><div class="isotope-feature-image"><?php the_post_thumbnail() ?></div>
                                        <h3 class="isotope-title"><a href="<?php the_permalink()?>"><?php the_title(); ?></a></h3>
                                        <p class="isotope-number">Post No: <?php the_ID(); ?></p>
                                        <p class="isotope-name">Author: <?php the_author() ?></p>
                                        <p class="isotope-type">Blog Type: <?php the_category() ?></p>
                                        <p class="isotope-datetime">Post Date: <?php the_date() ?> at <?php the_time() ?></p>
                                        <p class="isotope-blog"><?php the_content(); ?></p>
                                    </div>
                                </div>
                        <!-- Isotope Grid-Item -->    
                        <?php 
                    Endwhile;
                Endif;
            ?>
            </div>
        <!-- End of Isotope Grid -->

        <!-- Begin of Footer -->

        </div>
    </body>
  </html> 
  <!-- End of Footer -->

JS File (isotope.js):

"use strict";

(function ($){

  $.fn.isotope_gallery = function(){    

        jQuery( function() {
            var $container = jQuery('.isotope-grid');
            // use imagesLoaded, instead of window.load
            $container.imagesLoaded( function() {
                $container.isotope({
                    itemSelector: '.isotope-item',
                    // masonry is default layoutMode, no need to specify it
                });
            })
        });

        // init Isotope
        var $grid = $('.isotope-grid').isotope({
          itemSelector: '.isotope-grid-item',
          layoutMode: 'fitRows',
          isFitWidth: true,
          getSortData: {
            name: '.isotope-name',
            title: '.isotope-title',
            details: '.isotope-details',
            symbol: '.isotope-symbol',
            type: '.isotope-type',
            number: '.isotope-number',
            category: '[data-category]',
            //you can add hear more sorting class like those
          }
        });

        // filter functions
        var filterFns = {
          // show if number is greater than 50
          numberGreaterThan50: function() {
            var number = $(this).find('.number').text();
            return parseInt( number, 10 ) > 50;
          },
          // show if name ends with -ium
          ium: function() {
            var name = $(this).find('.name').text();
            return name.match( /ium$/ );
          }
        };

        // bind filter button click
        $('#isotope-filters').on( 'click', 'button', function() {
          var filterValue = $( this ).attr('data-filter');
          // use filterFn if matches value
          filterValue = filterFns[ filterValue ] || filterValue;
          $grid.isotope({ filter: filterValue });
        });

        // bind sort button click
        $('#isotope-sorts').on( 'click', 'button', function() {
          var sortByValue = $(this).attr('data-sort-by');
          $grid.isotope({ sortBy: sortByValue });
        });

        // change is-checked class on buttons
        $('.isotope-button-group').each( function( i, buttonGroup ) {
          var $buttonGroup = $( buttonGroup );
          $buttonGroup.on( 'click', 'button', function() {
            $buttonGroup.find('.is-checked').removeClass('is-checked');
            $( this ).addClass('is-checked');
          });
        });




        // Select and loop the container element of the elements you want to equalise
        $('.isotope-grid').each(function(){  

          // Cache the highest
          var highestBox = 0;

          // Select and loop the elements you want to equalise
          $('.isotope-grid-item', this).each(function(){

            // If this box is higher than the cached highest then store it
            if($(this).height() > highestBox) {
              highestBox = $(this).height(); 
            }

          });  

          // Set the height of all those children to whichever was highest 
          $('.isotope-grid-item',this).height(highestBox);

        }); 



  }

  $(this).isotope_gallery();


}(jQuery));

Thanks for your help!

1 Answer 1

0

Can you try:

<?php if (have_posts()): ?>
    <?php while (have_posts()): the_post(); ?>
        <?php
        $categories     = get_the_category();
        $listCategories = [];
        foreach ($categories as $category) {
            $listCategories[] = $category->slug;
        }
        ?>
        <!-- Isotope Grid-Item -->
        <div class="col-md-3 col-sm-4 col-xs-12 isotope-grid-item <?php echo implode(' ', $listCategories); ?>" data-category="<?php echo implode(',', $listCategories); ?>">
            <div class="isotope-item">
                <a href="#">
                    <div class="isotope-feature-image"><?php the_post_thumbnail() ?></div>
                    <h3 class="isotope-title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
                    <p class="isotope-number">Post No: <?php the_ID(); ?></p>
                    <p class="isotope-name">Author: <?php the_author() ?></p>
                    <p class="isotope-type">Blog Type: <?php the_category() ?></p>
                    <p class="isotope-datetime">Post Date: <?php the_date() ?> at <?php the_time() ?></p>
                    <p class="isotope-blog"><?php the_content(); ?></p>
                </a>
            </div>
        </div>
        <!-- Isotope Grid-Item -->
    <?php endwhile; ?>
<?php endif; ?>

Posts can have more than one category, if you look at the source you will probably see [array] or if you put PHP error reporting on you will get an error.

6
  • 1
    Hi and thank you for your help, I tried to replace my line of code with yours, my thumbnails no longer appear and I have an error instead and here is what it looks like: vgamerz.fr/isotopehelp/uncaught-error.html
    – Frenchy35
    Commented Apr 28, 2020 at 18:08
  • Saw the error Uncaught Error: Object of class WP_Term could not be converted to string please see my updated answer.
    – Drmzindec
    Commented Apr 30, 2020 at 9:45
  • Hi, I tried your code but my thumbnails still don't appear and when I click on the buttons I don't have a url.
    – Frenchy35
    Commented Apr 30, 2020 at 13:00
  • Hi, I tried your code but my thumbnails still don't appear and when I click on the buttons I don't have a url. The error comes from there: data-category="<?php echo (',', get_the_category($listCategories)); ?>">, otherwise the request seems correct to me but there is no filtration of the articles by categories.
    – Frenchy35
    Commented Apr 30, 2020 at 13:06
  • I realised something a bit late, apologies. You are using the css classes to filter so the <?php echo implode(',', get_the_category($listCategories)); ?> needs to echo there. The issue with your thumbnails i am unsure about, I tested it on my side and the thumbnails are working. please try the updated code.
    – Drmzindec
    Commented Apr 30, 2020 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.