Skip to content

Instantly share code, notes, and snippets.

@KittenCodes
Created February 4, 2022 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KittenCodes/45c5ddf8c43aa59a00f812767cf5d0ed to your computer and use it in GitHub Desktop.
Save KittenCodes/45c5ddf8c43aa59a00f812767cf5d0ed to your computer and use it in GitHub Desktop.
PHP to show WooCo Parent Categories only
<?php
$terms = get_terms(['taxonomy' => 'product_cat', 'hide_empty' => false, 'parent' => 0]);
if ($terms)
{ ?>
<div id="-woo-product-categories-3-137" class="oxy-woo-product-categories oxy-woo-element">
<div class="woocommerce columns-4">
<ul class="products columns-4">
<?php
foreach ($terms as $term)
{
//var_dump($term);
// GET IMAGE
// get the thumbnail id
$thumbnail_id = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
// get the image URL
$image = wp_get_attachment_url($thumbnail_id);
$srcset = wp_get_attachment_image_srcset($thumbnail_id, array(
300,
300
));
if (empty($image))
{
$image = '/wp-content/uploads/woocommerce-placeholder-300x300.png';
}
// GET NAME
$cat_name = $term->name;
// GET URL
$cat_url = get_term_link($term->term_id, 'product_cat');
?>
<li class="product-category product first">
<a href="<?php echo $cat_url; ?>">
<img src="<?php echo $image; ?>" srcset="<?php echo esc_attr($srcset); ?>" alt="<?php echo $cat_name; ?>" width="300" height="300">
<h2 class="woocommerce-loop-category__title">
<?php echo $cat_name; ?>
</h2>
</a>
</li>
<?php
}
?>
</ul>
</div>
</div>
<?php
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment