Problem: I needed to display sub categories of a category in category.php but only one level deep. Using:
<?php if(shopp('category','hascategories')): ?> <?php while(shopp('category','subcategories')): ?> //display stuff <?php endwhile; ?> <?php endif; ?> |
…displays all subcast AND their subcats.
Fix: I decided to add a new tag, called shopp(‘subcategory’, ‘depth’) that would return the category depth. That way in the loop above this would show only immediate children:
<?php if(shopp('category','hascategories')): ?> <?php while(shopp('category','subcategories')): $depth = shopp('subcategory', 'depth', 'return=true'); if($depth!=0): continue; endif; ?> //else display stuff <?php endwhile; ?> <?php endif; ?> |
Adding the new tag in Shopp 1.1: Open the theme’s functions.php file and add this:
add_filter('shopp_tag_category_depth','return_cat_depth',10, 3); add_filter('shopp_tag_subcategory_depth','return_cat_depth',10, 3); function return_cat_depth($result, $options, $Category) { return $Category->depth; } |
Adding the new tag in Shopp 1.2 and 1.3+: Open the theme’s functions.php file and add this:
add_filter('shopp_themeapi_collection_depth','lookup_my_depth',10, 3); function lookup_my_depth ($result, $options, $Category) { $ancestors = get_ancestors($Category->id, 'shopp_category' ); return(count($ancestors)); } |
…once that’s added, shopp(‘subcategory’, ‘depth’) can be used anywhere in the Shopp templates.
Please let me know if this post was useful with a click, be honest 🙂
So what would that 1.2 hook look like?
This might be the latest reply ever… but I’ve just updated this with some code for the current version of Shopp 🙂