Last active
December 4, 2021 15:44
-
-
Save delennerd/ead807a6165583bad22d15b9f99d8eb0 to your computer and use it in GitHub Desktop.
WooCommerce - Display subcategories only on main category
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// This function is used to display manufactures of products | |
// With ACF create a true/false field "show_manufacturer" | |
// Create main categories and subcategories (manufactures) and add a logo | |
/** | |
* Display subcategories of a main category - using ACF fields | |
* @author Pascal Lehnert | |
* @copyright 2021-02-28 | |
*/ | |
add_action( 'woocommerce_archive_description', 'DLM_wc_archive_add_sub_categories_images', 15 ); | |
function DLM_wc_archive_add_sub_categories_images() { | |
if ( ! is_product_category() ) return false; | |
$category = get_queried_object(); | |
$term_id = $category->term_id; | |
$taxonomy = 'product_cat'; | |
// Get subcategories of the current category | |
$terms = get_terms([ | |
'taxonomy' => $taxonomy, | |
'hide_empty' => false, | |
'parent' => get_queried_object_id() | |
]); | |
if ( !get_field('show_manufacturer', $category) ) { | |
return; | |
} | |
$list_items = ''; | |
// Loop through product subcategories WP_Term Objects | |
foreach ($terms as $term) { | |
$term_link = get_term_link($term, $taxonomy); | |
$thumbnail_id = get_term_meta($term->term_id, 'thumbnail_id', true); | |
$image_url = wp_get_attachment_url($thumbnail_id); | |
$list_items .= '<li class="col-6 col-sm-4 col-md-3 cat-img cat-'. $term->slug .'"><a href="'. $term_link .'"><img src="'. $image_url .'" alt="'. $term->name .'">'. $term->name .'</a></li>'; | |
} | |
?> | |
<div class="container subcategories-wrapper"> | |
<h4><?php esc_html_e( sprintf( 'Unsere %s Marken:', $category->name ) , 'my_theme' ); ?></h4> | |
<ul class="subcategories-list row"> | |
<?php echo $list_items; ?> | |
</ul> | |
</div><!-- end .container --> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment