Adding category depth tag to Shopp 1.1.*

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: 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;}

…once that’s added, shopp(‘subcategory’, ‘depth’) can be used anywhere in the Shopp templates.

Note: In Shopp 1.2 the hook is created differently. Thanks to Jon Davies for pointing out that the hook would now be shopp_themeapi_{object name}_{property} and defined in defined in shopp/api/theme.php.

Leave a Reply