WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to diffentiate categories and sub-categories on a filter dropdown (3 posts)

  1. michaellawrence
    Member
    Posted 1 year ago #

    Hi, I have a site with a Custom Post Type for wholesale products. When you enter in a product, you can check categories and subcategories (like Handbag, Jewelry, etc. And below handbag you can check Tote Bag or Beach Bag).

    I have this code that allows the user to filter through the list.

    <?php 
    
    // check if we got posts to display:
    if (have_posts()) :
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
    
    $args = array("post_type" => "wholesale",
          "orderby" => "date",
    	  "posts_per_page" => "20",
          "paged" => $paged
       );
    
    global $sortby;
    $sortby = $_GET['sortby'];
    if ($sortby == "title") {
        $args["orderby"] = "title";
    }
    
    global $genre_filter;
    $genre_filter = $_GET['genre'];
    if ($genre_filter) {
        $args['tax_query'] = array(array("taxonomy" => "wholesale_cats", "terms" => $genre_filter, "field" => "slug", "hierarchical" => "1" ));
    }
    
    query_posts($args);
    ?>
    
    <div class="wholesale-filter">
    	<span>FILTER:</span>
    
    	<?php global $genre_filter, $sortby; ?>
    
    	<a class="genre_all" href="?sortby=<?php echo $sortby; ?>">ALL</a> <a class="sort_title" href="?sortby=title&genre=<?php echo $genre_filter; ?>">A-Z</a>
    
    	<script language="JavaScript" type="text/JavaScript">
    	<!--
    	function MM_jumpMenu(targ,selObj,restore){ //v3.0
    	  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
    	  if (restore) selObj.selectedIndex=0;
    	}
    	//-->
    	</script>
    
    	<form name="form1" id="form1" class="wholesale-dropdown">
    	  <select name="menu1" onchange="MM_jumpMenu('parent',this,0)">
    		<option value="Make a selection">--- make a selection ---</option>
    		<option value="?sortby=<?php echo $sortby; ?>">--- All ---</option>
    		<?php
    		     $genres = get_terms("wholesale_cats");
    		     if ($genres) {
    			 foreach ($genres  as $genre) {
    			 //	      print_r($genre);
    				 echo '<option value="?view_mode='.$_GET['view_mode'].'&genre='. $genre->slug .'&sortby='.$sortby.'">' . $genre->name . '</option>';
    			 }
    		     }
    		?>
    	  </select>
    	</form>

    This works great, but shows all categories and sub categories all in one drop down. How do I get it so sub categories have an indent in their visual display?

  2. Archie22is
    Member
    Posted 1 year ago #

    Hi,

    Did you come right? I have the following code on my side:

    <!-- Filter per sub-category -->
    			<h3>Filter your results</h3>
    			<?php
    				//
    				$this_category = get_category(get_query_var('cat'),false);
    				$cat_ID = (int)get_query_var('cat');
    				$results = get_categories('&child_of='.$cat_ID.'&hide_empty');
    			?>	
    
    			<select name="sub-filter" ONCHANGE="location = this.options[this.selectedIndex].value;">
    			<?php
    				foreach ($results as $res){
    					echo '<option value= "';
    					echo network_site_url() . '/category/' . $mylink->slug . '/' . $res->slug ;
    					echo '"'. '>';
    					echo $res->name;
    					echo '</option>';
    
    					var_dump($mylink);
    				}
    			?>
    			</select>

    What happens there, I have used the default category listing. The top category takes you to the parents taxonomy menu (www.site.com/category/chocolate).

    From the above link, you get a dropdown list with all the chocolates (black chocolate, etc). However I cannot get the above code to filter some more. How did you work your way around the code?

  3. michaellawrence
    Member
    Posted 1 year ago #

    Hi, my code was generated by a plugin from Woocommerce, so I didn't write it myself.

Topic Closed

This topic has been closed to new replies.

About this Topic