Inspired from get woocommerce categories with subcategory, I am creating a drop down section and I was wondering if there is any way to remove the permalink from the parent category if it has a child category.
Here is my code example:
<?php
$args = array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
'parent' => 0
);
$product_cat = get_terms( $args );
$thumbnail_id = get_woocommerce_term_meta( $term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
foreach ($product_cat as $term){
$term_link = get_term_link( $term, 'product_cat' );
$thumb_id = get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true );
$img_src = wp_get_attachment_url( $thumb_id );
//if parent category has children remove link
echo '<ul>
<li><img src="' . $img_src . '"/><a href="'.get_term_link($term->term_id).'">'.$term->name.'</a>
<ul>';
// else keep the parent link
$child_args = array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
'parent' => $term->term_id
);
$child_product_cats = get_terms( $child_args );
foreach ($child_product_cats as $child_product_cat){
echo '<li><a href="'.get_term_link($child_product_cat->term_id).'">'.$child_product_cat->name.'</a></li>';
}
echo '</ul>
</li>
</ul>';
}
How can I display a link on top level product categories terms only when there are no children terms?
The following will display a list of product category terms where the top level term will not be linked if they have any child term:
Tested and works.