M
M
midas342019-12-09 18:49:26
Taxonomy
midas34, 2019-12-09 18:49:26

How to display sections and subsections of woocomerce products?

There is this code:

<?php
  $args = array(
        'taxonomy'   => 'product_cat',
        'hide_empty' => false,
        'parent'   => 0,
      );
      $product_categories = get_terms( $args );

      $count = count($product_categories);

      if ( $count > 0 ){
        foreach ( $product_categories as $product_category ) {
            $thumbnail_id = get_woocommerce_term_meta( $product_category->term_id, 'thumbnail_id', true );
            echo '<div class="my-vid-category">';
            echo '<ul class="my-vid">';
            echo  '<li class="my-entry-block">';
            echo  '<div class="my-entry-title">';
            echo  '<a href="' . get_term_link( $product_category ) . '" title="' . $product_category->name . '"><h2>' . $product_category->name . '</h2></a>';
            echo  '</div>';
            echo '<article class="my-type-post">';
            echo '<div class="my-entry-cover" style="width: 450px; height: 300px;">';
            echo  '<a href="' . get_term_link( $product_category ) . '"><img style="background-image: url('.  wp_get_attachment_url( $thumbnail_id ) .')!important;" /></a>';
            echo  '</div>';
            echo  '</article>';
            echo  '<hr>';
            echo  '</li>';
            echo  '</ul>';
            echo  '</div>';
        }
      }
?>

It generates a list of parent categories,
a title block section and an IMG block
how you can add more and display child subcategories with links below IMG

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
midas34, 2019-12-10
@midas34

Issue resolved! Apply styles, the result will be as follows: category display (can be displayed in columns and rows),
hierarchy: Title (link) => Image (link) => subcategories (link), thanks to css you can make a very good catalog with navigation

<?php
  $args = array(
        'taxonomy'   => 'product_cat',
        'hide_empty' => false,
        'parent'   => 0,
      );
      $product_categories = get_terms( $args );

      $count = count($product_categories);

      if ( $count > 0 ){
        foreach ( $product_categories as $product_category ) {
            $thumbnail_id = get_woocommerce_term_meta( $product_category->term_id, 'thumbnail_id', true );
            echo '<div class="my-vid-category">';
            echo '<ul class="my-vid">';
            echo  '<li class="my-entry-block">';
            echo  '<div class="my-entry-title">';
            echo  '<a href="' . get_term_link( $product_category ) . '" title="' . $product_category->name . '"><h2>' . $product_category->name . '</h2></a>';
            echo  '</div>';
            echo '<article class="my-type-post">';
            echo '<div class="my-entry-cover" style="width: 450px; height: 300px;">';
            echo  '<a href="' . get_term_link( $product_category ) . '"><img style="background-image: url('.  wp_get_attachment_url( $thumbnail_id ) .')!important;" /></a>';
            $args = array(
                'taxonomy'   => 'product_cat',
                'hide_empty' => false,
                'parent'   => $product_category->term_id,
              );
              $product_categories = get_terms( $args );
        
              $count = count($product_categories);
        
              if ( $count > 0 ){
                foreach ( $product_categories as $product_category ) {
                 echo  '<div class="my-parent-title">';
                 echo  '<a href="' . get_term_link( $product_category ) . '" title="' . $product_category->name . '"><h4>' . $product_category->name . '</h4></a>'; 
                 echo  '</div>';
            }}
            echo  '</div>';
            echo  '</article>';
            echo  '<hr>';
            echo  '</li>';
            echo  '</ul>';
            echo  '</div>';
        }
      }
?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question