Skip to content

Instantly share code, notes, and snippets.

@jorpdesigns
Created July 12, 2021 13:51
Show Gist options
  • Select an option

  • Save jorpdesigns/6a8a0629f5dffd236e9143f77ae4e35c to your computer and use it in GitHub Desktop.

Select an option

Save jorpdesigns/6a8a0629f5dffd236e9143f77ae4e35c to your computer and use it in GitHub Desktop.
Function to get WooCommerce product categories
<?php
function get_product_categories() {
// Parameters available here: https://developer.wordpress.org/reference/classes/wp_term_query/__construct/#parameters
$args = array(
'taxonomy' => 'product_cat',
'child_of' => 5, // Get child terms of category with ID 5
'parent' => 5, // Get direct-child terms of category with ID 5
);
$categories = get_categories($args);
foreach ($categories as $category) {
$categoryID = $category->term_id;
$categoryName = $category->name;
$categorySlug = $category->slug;
$categoryTaxonomy = $category->taxonomy;
$categoryDescription = wpautop( $category->description );
$categoryParent = $category->parent;
$categoryParents = get_ancestors($categoryID, $categoryTaxonomy);
$categoryCount = $category->count;
$thumbnail_id = get_term_meta( $category->term_id, 'thumbnail_id', true);
$categoryImage = wp_get_attachment_image($thumbnail_id, $size = 'full');
$categoryLink = get_category_link( $category->term_id );
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment