最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - products nested by subcategories in a current category archive

programmeradmin1浏览0评论

I am able to get a list of its subcategories in a category archive page using below code in archive-product.php but I want to display the list of the products currently assigned to each subcategory.

<?php
    $parentid = get_queried_object_id();
    $args = array(
        'parent' => $parentid
    );
    $categories = get_terms(
        'product_cat', $args
    );
    if ( $categories ) {
        foreach ( $categories as $category ) {
            echo $category->name;
        }
    }
?>

Example:

Subcategory 1

  • Product 1 of Subcategory 1
  • Product 2 of Subcategory 1


Subcategory 2

  • Product 1 of Subcategory 2
  • Product 2 of Subcategory 2

I am still learning WordPress so any help will be highly appreciated.

I am able to get a list of its subcategories in a category archive page using below code in archive-product.php but I want to display the list of the products currently assigned to each subcategory.

<?php
    $parentid = get_queried_object_id();
    $args = array(
        'parent' => $parentid
    );
    $categories = get_terms(
        'product_cat', $args
    );
    if ( $categories ) {
        foreach ( $categories as $category ) {
            echo $category->name;
        }
    }
?>

Example:

Subcategory 1

  • Product 1 of Subcategory 1
  • Product 2 of Subcategory 1


Subcategory 2

  • Product 1 of Subcategory 2
  • Product 2 of Subcategory 2

I am still learning WordPress so any help will be highly appreciated.

Share Improve this question edited Jul 29, 2018 at 13:44 Krzysiek Dróżdż 25.5k9 gold badges53 silver badges74 bronze badges asked Jul 29, 2018 at 10:35 Kevin SKevin S 133 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1
<?php
    $parentid = get_queried_object_id();
    $args = array(
        'parent' => $parentid
    );
    $categories = get_terms(
        'product_cat', $args
    );
    if ( $categories ) :
        foreach ( $categories as $category ) :
            echo esc_html($category->name);
            $products = new WP_Query( array(
                'post_type' => 'product',
                'tax_query' => array(
                    array(
                        'taxonomy' => 'product_cat',
                        'field' => 'slug',
                        'terms' => $category->slug,
                    ),
                ) 
            ) );
            if ( $products->have_posts() ) :
            ?>
                <ul>
                    <?php while ( $products->have_posts() ) : $products->the_post(); ?>
                        <li><?php the_title(); ?></li>
                    <?php endwhile; ?>
                </ul>
            <?php
                wp_reset_postdata();
            endif;
        endforeach;
    endif;
?>

here is my code for this. You can find it here: [Archive category clustered by child categories][1]

It also displays number of posts in child category and link to child category.

$term = get_queried_object(); // Get current archive page
$children = get_terms($term->taxonomy, array(
  'parent'    => $term->term_id,
  'hide_empty' => false
));

if ($children) {
  foreach ($children as $subcat) {
    echo '<h2><a href="' . esc_url(get_term_link($subcat, $subcat->taxonomy)) . '">' . // Display child taxonomy name
      $subcat->name . ' (' . $subcat->count . ')' . '</a></h2>' . $subcat->description; // Display child taxonomy item count
    $kategorie = $subcat->slug; // Set child category slug for each query of products
    $args = array(
      'post_type' => 'product',
      'product_cat' => $kategorie, 
    );
    $loop = new WP_Query($args);
    if ($loop->have_posts()) {
      while ($loop->have_posts()) : $loop->the_post(); ?>
        <h4><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); //Display product title ?></a></h4>
        <?php $product = wc_get_product(get_the_ID()); // get the WC_Product Object ?>
        <p><?php echo $product->get_price_html(); // Display product Price ?></p>
        <?php endwhile; ?><?php
                        } else {
                          echo __('No products found');
                        }
                        wp_reset_postdata(); // Reset Query
                          ?>
        <!--/.products-->
    <?php

  }
}
    ?>


  [1]: https://toolset.wiki/article/woocommerce-category-clustered-by-child-taxonomies-in-wordpress/
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>