I
I
Ilya Derevyannykh2021-12-16 15:14:40
WooCommerce
Ilya Derevyannykh, 2021-12-16 15:14:40

How to display categories in all categories and directory?

There is code for output in categories only. How to modernize it so that all categories are displayed in the catalog and all other categories are displayed in child categories (for example, in children's mattresses so that TFK, springless and other child categories are displayed)

/* Выводим категории над списком товаров */
function woocommerce_product_category( $args = array() ) {
  $woocommerce_category_id = get_queried_object_id();
  $args = array(
    	'parent' => $woocommerce_category_id
  );
  $terms = get_terms( 'product_cat', $args );
  if ( is_product_category() ) {
  if ( $terms ) {
    	echo '<div class="my-category"><div><ul class="woocommerce-categories">';
    	foreach ( $terms as $term ) {
        	echo '<li class="woocommerce-product-category-page">';
      echo '<a href="' .  esc_url( get_term_link( $term ) ) . '" class="' . $term->slug . '">';
            woocommerce_subcategory_thumbnail( $term );
      echo '</a>';
      echo '<p>';
        	echo '<a href="' .  esc_url( get_term_link( $term ) ) . '" class="' . $term->slug . '">';
        	echo $term->name;
        	echo '</a>';
        	echo '</p>';
        	echo '</li>';
    	}
    	echo '</ul></div></div>';
  	}
  }
}

add_action( 'woocommerce_before_main_content', 'woocommerce_product_category', 100 );

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question