Skip to content

Instantly share code, notes, and snippets.

@wickywills
Last active January 31, 2020 09:49
Show Gist options
  • Save wickywills/7ccaf1029b0aee2dca96a0fe7c037655 to your computer and use it in GitHub Desktop.
Save wickywills/7ccaf1029b0aee2dca96a0fe7c037655 to your computer and use it in GitHub Desktop.
Check if current archive is a category listing or if it is the lowest level category, in which case, list products. Useful for having different styles for products and categories.
```
/**
* Determine if a listing page us a category with subcategories, or a category with products
* @return string
*/
function get_wc_list_type() {
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); // get current term
$parent = get_term($term->parent, get_query_var('taxonomy') ); // get parent term
$children = get_term_children($term->term_id, get_query_var('taxonomy')); // get children
// If there's a parent, then proceed with further checks
if (!empty($parent) && !is_wp_error($parent)) {
if( ($parent->term_id != "" && sizeof($children) > 0) ) {
return "category";
} elseif ( ($parent->term_id != "") && (sizeof($children) == 0) ) {
return "products";
} elseif ( ($parent->term_id == "") && (sizeof($children) > 0) ) {
return "category";
}
} else { // If there is no parent, assume top level category (THIS NEEDS FURTHER WORK, since we shouldn't assume)
if( sizeof($children) == 0 ){
return "products";
} else {
return "category";
}
}
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment