Updated for 1.2 & 1.3: Adding category depth tag to Shopp

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 🙂
Nope, not helpful...Yep, more useful than not! (No Ratings Yet)
Loading...

2 thoughts on “Updated for 1.2 & 1.3: Adding category depth tag to Shopp”

    1. This might be the latest reply ever… but I’ve just updated this with some code for the current version of Shopp 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *