I have written some code that uses the Wordpress get_terms function to display a list of sub-categories for a user-defined category and also shows the relevant thumbnail for each sub-category. As far as I'm aware, there isn't a WP function to display all child categories across multiple parents, so I was wondering if it's possible to merge the results of two or more get_terms results?
The code I have written so far is working fine to get_terms from just one parent category but I'm not sure where to go from here…
function get_wc_child_cat_thumbs($catParent, $listClassName) {
// Our wordpress get_terms arguments
$args = array(
'number' => $number,
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
'include' => $ids,
'parent' => $catParent,
);
// Get the terms
$product_categories = get_terms( 'product_cat', $args );
// See if any results are returned
$count = count($product_categories);
// If there are, populate a list with the subcategories details
if ( $count > 0 ){
echo '<ul class="'.$listClassName.'">';
foreach ( $product_categories as $product_category ) {
// Get the thumbnail id for the subcategory
$thumbnail_id = get_woocommerce_term_meta( $product_category->term_id, 'thumbnail_id', true );
// Show the results…
echo '<li>'.wp_get_attachment_image( $thumbnail_id ).'<br />'.$product_category->term_id.' - <a href="' . get_term_link( $product_category ) . '">' . $product_category->name . '</a></li>';
//echo $image = wp_get_attachment_image( $thumbnail_id );
}
echo '</ul>';
} // END if ( $count > 0 ){
} // END function get_wc_child_cat_thumbs`
Is it possible to change the $catParent
argument so that it accepts either single or array values and add in an array_merge somewhere?
I managed to sort the problem (literally) with great help from @RST. In the end I merged the multiple arrays and sorted the results alphabetically using usort and the name value from the array as the comparison. Here's the finished code…