Hi Fernando,
Thanks for coming back again.
One of the two sub-categories that was displaying (out of 13) had products assigned, the other had products assigned to its sub-categories. It is a puzzler what causes the problem; I wondered about database collation as the data has originally come from Joomla 1.5 in a roundabout route.
Thank you for your help. I was completely stuck! I have now created a workaround which displays all subcategories (and the header category image). I have done this through a Plugin – see below.
<?php
/**
* Plugin Name: Squirrelhouse - handle WooCommerce (sub)categories in archive pages
* Description: Display products and categories / subcategories as two separate lists in product archive pages
* Version: 1.0
* Author: Squirrelhouse
* Author URI: https://media.squirrelhouse.biz
*/
/**
* Display all sub categories based on 3 column display
* Based on https://code.tutsplus.com/tutorials/display-woocommerce-categories-subcategories-and-products-in-separate-lists--cms-25479
*/
function squirrel_product_subcategories( $args = array() ) {
$parentid = get_queried_object_id();
$counter = 1; //counter tracks the number of the post we're on
$args = array(
'parent' => $parentid
);
$terms = get_terms( 'product_cat', $args );
if ( $terms ) {
echo '<hr><h2 class="squirrel-cat-section">Sub-Categories</h1>';
echo '<ul class="products columns-3">';
foreach ( $terms as $term ) {
// add last or first classes
?>
<?php if ($counter % 3 == 0) { echo '<li class="product-category product squirrel-cat last">';}
elseif ($counter % 3 == 1) { echo '<li class="product-category product squirrel-cat first">';}
else { echo '<li class="product-category product squirrel-cat">';}
?>
<?php
// echo '<li class="product-category product squirrel-cat ">';
echo '<a href="' . esc_url( get_term_link( $term ) ) . '" class="' . $term->slug . '" squirrel-cat-a>';
woocommerce_subcategory_thumbnail( $term );
echo '<h2 class="woocommerce-loop-category__title squirrel-cat-h2">';
echo $term->name;
echo '</h2>';
echo '</a>';
echo '</li>';
$counter++; // This increases the value of $counter by 1 for every loop iteration
}
echo '</ul>';
echo '<hr><h2 class="squirrel-cat-section">Products</h1>';
}
}
add_action( 'woocommerce_before_shop_loop', 'squirrel_product_subcategories', 50 );
/**
* Display category image on category archive
* Based on https://stackoverflow.com/questions/12717112/how-to-display-woocommerce-category-image
*/
function woocommerce_category_image() {
if ( is_product_category() ){
global $wp_query;
$cat = $wp_query->get_queried_object();
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
if ( $image ) {
echo '<img src="' . $image . '" alt="' . $cat->name . '" />';
}
}
}
add_action( 'woocommerce_archive_description', 'woocommerce_category_image', 2 );
?>