Link to home
Start Free TrialLog in
Avatar of Member_2_7966984
Member_2_7966984

asked on

I want to be able to use the Isotobe filter URLs in my main nav link - but currently only filters data when link is clicked

So I have a page that uses jquery Isotope filtering https://buildingalifestyle.com/floor-plans/ - and the filters are working when you click the filter links - I noticed when you click the filter links the URL of the page changes to include the filtered results - however if I copy this URL and paste it in my main navigation - when the page loads the results are not filtered unless I click the filter link - So basically I want to display some filtered results as an item in my main site navigation when clicked the page would load just as if I had clicked the filter link which turns into this when clicked -  https://buildingalifestyle.com/floor-plans/#filter-lot-3

So I added some items to my main navigation under "Floor Plans" called Executive and Signature

signature = https://buildingalifestyle.com/floor-plans/#filter-lot-3
executive= https://buildingalifestyle.com/floor-plans/#filter-lot-2

So ideally I would like these results to be filtered when the page loads so the filters could essentially be triggered by clicking a link in the the main navigation menu.

This is what my code currently looks like - Isotope filter code is at the bottom
 

<?php
/**
 * Template Name: Page Find Your Home
 * A full-width template.
 *
 * @package Avada
 * @subpackage Templates
 */


// Do not allow directly accessing this file.
if ( ! defined( 'ABSPATH' ) ) {
   exit( 'Direct script access denied.' );
}
?>
      <?php get_header(); ?>
         
         <div id="content">


            <div  class="content2">


               <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
               
               <article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article" itemscope itemtype="http://schema.org/BlogPosting">
                  
         
               
                  <section class="post_content clearfix" itemprop="articleBody">
                     <?php the_content(); ?>
               
                  </section> <!-- end article section -->
                       


<section id="options" class="clearfix combo-filters">
 
     <div class="filter-choice">
        <ul class="filter option-set clearfix " data-filter-group="bed">
          <li><a href="#filter-color-any" data-filter="" class="selected">any</a>
         
            <li><a href="#filter-bed-3" data-filter=".3-bed">3 Bedrooms</a></li>
         
            <li><a href="#filter-bed-4" data-filter=".4-bed">4 Bedrooms</a></li>
         
         
        </ul>
      </div>
     
     
   <div class="filter-choice">
        <ul class="filter option-set clearfix " data-filter-group="bath">
          <li><a href="#filter-color-any" data-filter="" class="selected">any</a>
         
            <li><a href="#filter-bath-2" data-filter=".2-bath">2 Bathrooms</a></li>
            <li><a href="#filter-bath-3" data-filter=".3-bath">3 Bathrooms</a></li>
         
            <li><a href="#filter-bath-4" data-filter=".4-bath">4 Bathrooms</a></li>
         
         
        </ul>
      </div>
     
     
         <div class="filter-choice">
        <ul class="filter option-set clearfix " data-filter-group="gar">
          <li><a href="#filter-gar-any" data-filter="" class="selected">any</a>
         
            <li><a href="#filter-gar-2" data-filter=".2-gar">2-Car Garage</a></li>
            <li><a href="#filter-gar-3" data-filter=".3-gar">3-Car Garage</a></li>
         
         
        </ul>
      </div>
     
 
      <div class="filter-choice">
      <ul class="filter option-set clearfix " data-filter-group="lot">
        <li><a href="#filter-lot-any" data-filter="" class="selected">any</a>
       
          <li><a href="#filter-lot-2" data-filter=".executive">Executive</a>
       
          <li><a href="#filter-lot-3" data-filter=".signature">Signature</a>
       
       
      </ul>
    </div>


</section> <!-- #options -->




<div id="plan-container" class="clearfix">
             <?php
                        $media_items = get_attachments_by_media_tags('media_tags=gallery&numberposts=24&return_type=li&size=full&post_parent='.$post->ID.'');
                        if ($media_items) {
                            echo $media_items;
                        }
                               ?>
      <?php
    //$mypost = array( 'post_type' => 'plan', );
   
    $mypost = array( 'post_type' => 'plan',    
    'tax_query'   => array(
        array(
            'taxonomy'  => 'custom_cat',
            'field'     => 'slug',
            'terms'     => array('alamanda-key','harmony','viera'), // exclude media posts in the news-cat custom taxonomy
            'operator'  => 'NOT IN')
            ),
           
            );
   
    $loop = new WP_Query( $mypost );
    ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post();?>
   
    <?php $lot = get_post_meta($post->ID, "wpcf-plan-type", true); //SWAP OUT "LOT" WITH THE NAME OF THE NEW CUSTOM FIELD ?>
    <? if($lot == "n") {$lot = "signature";} else {$lot = "executive";} // CHANGE $lot == "n" to whatever your first option is ?>
 
                <!-- Display featured image in right-aligned floating div -->
                <? if ($post->ID != 9000 )  { ?>
                <? $bathClass = ""; ?>
                <? if (get_post_meta($post->ID, "Bathrooms", true)=="2.5"){$bathClass = "2-bath 3-bath";} ?>
                <? if (get_post_meta($post->ID, "Bathrooms", true)=="3.5"){$bathClass = "3-bath 4-bath";} ?>                
                <div class="planGrid color-shape <?php echo get_post_meta($post->ID, "Bedrooms", true); ?>-bed <?php echo get_post_meta($post->ID, "Bathrooms", true); ?>-bath <? echo $bathClass; ?> <?php echo get_post_meta($post->ID, "Garage", true); ?>-gar <? echo $lot; ?>" data-bed="<?php echo get_post_meta($post->ID, "Bedrooms", true); ?>" data-bath="<?php echo get_post_meta($post->ID, "Bathrooms", true); ?>" data-gar="<?php echo get_post_meta($post->ID, "Garage", true); ?>" data-lot="<? echo $lot; ?>">
                <? } else {?>
               
                <div class="planGrid2 color-shape 3-bed 4-bed 2-bath 3-bath 4-bath 2-gar 3-gar executive signature list-position">
               
               <?  } ?>
                   <?php
//Get the Thumbnail URL
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 300,150 ), false, '' ); ?>
<a href="<? the_permalink(); ?>"><img src="<? echo $src[0]; ?>" width="300" height="150" /></a>
               
               
                    <div class="plan-name-wrapper">
                  <div class="house-title"><a href="<? the_permalink(); ?>"><? the_title(); ?></a></div>
                </div>
               
               
               
                </div>
 
               
   
    <?php endwhile; ?>
      </div>
       
   
 
 
</div> <!-- #container -->


<script>


   
// init Isotope
var $grid = jQuery('#plan-container').isotope({
  // options
  itemSelector: '.planGrid',
  layoutMode: 'fitRows'
});
// filter items on button click
jQuery('#options').on( 'click', 'a', function() {
  var filterValue = jQuery(this).attr('data-filter');
  $grid.isotope({ filter: filterValue });
});
 
</script>



Open in new window



Open in new window

Avatar of leakim971
leakim971
Flag of Guadeloupe image

Put this in a $(document).ready block
jQuery("#options a").each(function(i,el) { 
    if(jQuery(el).attr("href")==location.hash) {
        jQuery(el).click();
        return false;
    }
});

Open in new window

or at the end of your page in script tag
<script>

    jQuery("#options a").each((i,el) => {
        if(jQuery(el).attr("href")==location.hash) {
            jQuery(el).click();
            return false;
        }
    });
    
</script>
</body>
</html>

Open in new window

Avatar of Member_2_7966984
Member_2_7966984

ASKER

Thanks I added that code - So it seems to work if I open a new tab and paste this link in it - https://buildingalifestyle.com/floor-plans/#filter-lot-3 

However I have this set up in my wordpress menu as a custom link ... and when I try to click on it via the wordpress menu the sorting part doesn't work.

I have added this to right before the closing body tag
<script>
jQuery(document).ready(function($) {
   // Code that uses jQuery's $ can follow here.


jQuery("#options a").each(function(i,el) {
    if(jQuery(el).attr("href")==location.hash) {
        jQuery(el).click();
        return false;
    }
});


});
</script>

Open in new window

The signature category only has 5 listings in it so that is a good one to use as a test b/c the results are very obvious when it works
as far you create hash tag for each of theses submenu, that should work with the same logic
jQuery("#options a,"#menu-item-10999 .sub-menu a").each(function(i,el) {
So I changed it to this but still doesn't work

<script>
jQuery(document).ready(function($) {
   // Code that uses jQuery's $ can follow here.


jQuery("#menu-item-10999 .sub-menu a").each(function(i,el) {
    if(jQuery(el).attr("href")==location.hash) {
        jQuery(el).click();
        return false;
    }
});


});
</script>

Open in new window

if you remove that code, do ALL the isotobe link work fine ?

ok yeah, I noticed that the code was breaking the isotope links - So now that I have removed that code the isotope links work again.
ok so now, try the code provided
So perhaps I did not implement the code provided correctly ...

Could you please provide an example of how the the full code would look like using this provided code 

jQuery("#options a,"#menu-item-10999 .sub-menu a").each(function(i,el) {



I just tested your links and they still do different things (nothing, opening a new tab,...)
The code cannot work until the isotope work fine itself
So I see what is going on - the isotope filters is not working on https://buildingalifestyle.com/find-your-home/ but it is working on https://buildingalifestyle.com/floor-plans/ 

I click on "Signature Floor Plans", the URL change (https://buildingalifestyle.com/floor-plans/#filter-lot-3) but nothing in the results below
Same for the other link...

hmm maybe cached? - I just checked https://buildingalifestyle.com/floor-plans/ in incognito window and it seemed to be working for me 
tried incognito and no, no change (Chrome)
Very strange I'm using chrome and I tested on several other browsers as well and they are working for me if I visit https://buildingalifestyle.com/floor-plans/ and click on the red links above the thumbnail images - the ones in the nav are not working and that is what I am hoping to fix 
the ones in the nav are not working
I was on that.
the first code provided is dedicated to the other links
So How do I get the nav links to work with the filter URLs - I guess is my my question ...

So If I add this -
<script>
jQuery(document).ready(function($) {
   // Code that uses jQuery's $ can follow here.




jQuery("#options a").each(function(i,el) {
    if(jQuery(el).attr("href")==location.hash) {
        jQuery(el).click();
        return false;
    }
});




});
</script>   

Open in new window

then the filter link only seems to work if I open a new tab and paste the filter link in the address bar - then when the page loads it becomes filtered

but I would like the nav links to work in the same way - So then you sent this code

jQuery("#options a,"#menu-item-10999 .sub-menu a").each(function(i,el) { 

Open in new window

but I am uncertain exactly how to add this in order to make the nav links work when clicked - could you provide an example (full code) on how to integrate this code you sent above - in order to make the nav links work? 
I think you made two questions in one and I was focusing on one of them...
I would like to recommend you to create two posts instead as it totally different question IMO.

inside you document ready block add the following after your current code :

$("span:contains('Signature Floor Plans')").parent().on("click", function(event) {
       event.preventDefault();
       $("#options a[href*='filter-lot-3']").click();
});


$("span:contains('Executive Floor Plans')").parent().on("click", function(event) {
       event.preventDefault();
       $("#options a[href*='filter-lot-2']").click();
});

Open in new window

Thanks I think we are getting closer - So this is what my code looks like now

<script>
jQuery(document).ready(function($) {
   // Code that uses jQuery's $ can follow here.




jQuery("#options a").each(function(i,el) {
    if(jQuery(el).attr("href")==location.hash) {
        jQuery(el).click();
        return false;
    }
   
});
   
$("span:contains('Signature Floor Plans')").parent().on("click", function(event) {
       event.preventDefault();
       $("#options a[href*='filter-lot-3']").click();
});




$("span:contains('Executive Floor Plans')").parent().on("click", function(event) {
       event.preventDefault();
       $("#options a[href*='filter-lot-2']").click();
});   
   




});
</script>

Open in new window

and while viewing the page https://buildingalifestyle.com/floor-plans/ the links in the drop down menu work - but if I am on any other page page say the about page for example https://buildingalifestyle.com/about-us/ the links in the main drop down menu do not filter when clicked - Any idea how we might be able to get the filter links to work if coming from a different page ? 
do you understand what we did in the last code for those two links ?
No not entirely - would guess it would only work if the pages are inside the parent page element? So is it even possible to do what I want to do?
I'm guessing I will probably need to make a new page with a custom page template and use a loop to pull in the data if we can't make it so the filter URLs will work if clicked on from another page 
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.