Jump to content

Make categories a drop down menu

Recommended Posts

  • Replies 5
  • Views 1.4k
  • Created
  • Last Reply

Add to Home > Settings > Advanced > Code Injection > Header

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.0/jquery.min.js"></script>  
<script>
$(document).ready(function(){
  if (jQuery(window).width() <= 767) {
     $(document).ready(function() {
  var catItem = $('a.category-filter-link');
  var itemTag = $(catItem).html();
  var catDrop = "<select name='Categories' id='categories' class='catDrop'>";
  if (itemTag !== undefined) {
    catItem.each(function(i) {
      var fixLoc = $(this).html().trim().replace(/\s+/g, '+').replace(/&/g, '%26').replace('amp;', '');
      catDrop = catDrop + '<option value=' + fixLoc + '>' + $(this).html() + '</option>';
      i++;
    });
    catDrop = catDrop + "</select>";
    $(catDrop).insertAfter($('.category-filter-wrapper'));
  }
  var urlCat = window.location.href.split("?category=")[1];
  if (urlCat !== undefined) {
    var fixCat = urlCat.replace(/\%27/g, "'")
    $(".catDrop").val(fixCat);
  }
  $(".catDrop").change(function() {
    if ($(this).val() == "All") {
      window.location = "/";
    } else {
      window.location = "?category=" + $(this).val();
    }
  });
});

    }
});
</script>
<style>
@media screen and (max-width:767px) {
nav.category-filter-wrapper {
    display: none;
}
}
</style>

then save & refresh your site.

Code by @rwp

Email me if you have need any help (free, of course.). Answer within 24 hours. 
Or send to forum message

Contact Customer Care - Learn CSS - Buy me a coffee (thank you!)

Link to comment
  • 4 months later...

 

On 8/24/2020 at 5:00 PM, tuanphan said:

Add to Home > Settings > Advanced > Code Injection > Header



<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.0/jquery.min.js"></script>  
<script>
$(document).ready(function(){
  if (jQuery(window).width() <= 767) {
     $(document).ready(function() {
  var catItem = $('a.category-filter-link');
  var itemTag = $(catItem).html();
  var catDrop = "<select name='Categories' id='categories' class='catDrop'>";
  if (itemTag !== undefined) {
    catItem.each(function(i) {
      var fixLoc = $(this).html().trim().replace(/\s+/g, '+').replace(/&/g, '%26').replace('amp;', '');
      catDrop = catDrop + '<option value=' + fixLoc + '>' + $(this).html() + '</option>';
      i++;
    });
    catDrop = catDrop + "</select>";
    $(catDrop).insertAfter($('.category-filter-wrapper'));
  }
  var urlCat = window.location.href.split("?category=")[1];
  if (urlCat !== undefined) {
    var fixCat = urlCat.replace(/\%27/g, "'")
    $(".catDrop").val(fixCat);
  }
  $(".catDrop").change(function() {
    if ($(this).val() == "All") {
      window.location = "/";
    } else {
      window.location = "?category=" + $(this).val();
    }
  });
});

    }
});
</script>
<style>
@media screen and (max-width:767px) {
nav.category-filter-wrapper {
    display: none;
}
}
</style>

then save & refresh your site.

Code by @rwp

Hey,

Can I get help with this? Should I send an email?

Link to comment

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.