Skip to content

Instantly share code, notes, and snippets.

@Musilda
Created April 24, 2021 13:07
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 Musilda/3c11bcb8632b859e4e867a7690bb6bfc to your computer and use it in GitHub Desktop.
Save Musilda/3c11bcb8632b859e4e867a7690bb6bfc to your computer and use it in GitHub Desktop.
<?php
add_action( 'woocommerce_before_shop_loop', 'metaltrade_categories', 5 );
function metaltrade_categories(){
global $wp_query;
if( empty( $wp_query->queried_object->term_id ) ){
$parent = 0;
} else {
$parent = $wp_query->queried_object->term_id;
}
$args = array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
'parent' => $parent
);
$terms = get_terms( $args );
if( !empty( $terms ) ){
echo '<ul class="products columns-4 cat-term-wrap">';
$i = 1;
foreach( $terms as $term ){
$link = get_term_link( $term->term_id, 'product_cat' );
$thumbnail_id = get_term_meta( $term->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_image_url( $thumbnail_id, 'thumbnail' );
if( !empty( $image ) ) {
$class = 'term-image';
}else{
$class = 'term-noimage';
}
echo '<li class="category-loop product">';
echo '<a href="' . $link . '">';
if( !empty( $image ) ) {
echo '<img class="category-loop-thumbnail" src="' . $image . '" alt="' . $term->name . '" />';
}
echo '<h3 class="' . $class . '">' . $term->name . '</h3>';
echo '</a>';
echo '</li>';
}
echo '</ul>';
}
}
?>
<style>
.cat-term-wrap {
width:100%;
padding:0;
}
.category-loop a{
display:flex;
align-items: center;
}
.cat-term-wrap .category-loop .category-loop-thumbnail{
width:50px;
margin-bottom:0;
}
.cat-term-wrap .category-loop h3{
padding:0 10px 0 10px;
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment