doudeng2016 2017-08-17 08:07
浏览 153
已采纳

获取每个WooCommerce父类别的现有子类别的总数

I have edited woocommerce archive-product.php file already to achieve my layout but there is still one thing that i need to figure out. I want to display total number of existing subcategory in archive each page.

For example I have two main product categories (A, B) that each one of them has some subcategories:

Category A
    Subcategory A1
    Subcategory A2

Category B
    Subcategory B1
    Subcategory B2
    Subcategory B3

I want to show number of subcategories of each parent, in my case 2 subcategories in A archive page and 3 subcategories in B archive page.

This is what I've done so far :

    <?php
/**
 * The Template for displaying product archives, including the main shop page which is a post type archive
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/archive-product.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see         https://docs.woocommerce.com/document/template-structure/
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     2.0.0
 */

 include '/../templates/aq_resizer.php';


if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

get_header( 'shop' ); ?>

        <?php
             $term = $queried_object = get_queried_object(); 
    $taxonomy = $queried_object->taxonomy;
    $term_id = $queried_object->term_id;  
    $classes = get_body_class();
?>
    <?php if( get_field('top_image') ): ?>
                   <div  class="top-bg" style="background-image:url('<?php the_field('top_image', $taxonomy . '_' . $term_id); ?>'); ">
                           <div class="container">
                        <div class="filter-cat">
                                  <div id="search-container"  class="container">
                                           <?php echo do_shortcode( '[searchandfilter id="85"]' ); ?>
                                   </div>                   
                              </div>
                     </div>
                   </div>
           <?php else: ?>
                <div  class="top-bg" style="background-image:url('<?php bloginfo('template_url') ?>/images/Product-Page.jpg'); ">
                     <div class="container">
                        <div class="filter-cat">
                                  <div id="search-container"  class="container">
                                           <?php echo do_shortcode( '[searchandfilter id="85"]' ); ?>
                                          <?php 

function woocommerceCategorySlug( $id ){
    $term = get_term_by('id', $id, 'product_cat', 'ARRAY_A');
    return $term['slug'];       
}

?>
                                   </div>                   
                              </div>
                     </div>
                </div>
          <?php endif; ?>


            <h2 class="container taxonomy-title">

                   <?php if (in_array('tax-product_cat',$classes)): ?>

                                   <?php echo  $term->name ?>
            <?php else: ?>

                         <?php

                $url = $_SERVER["REQUEST_URI"];
                        $url = explode("=", $url);
                        $url = urldecode($url[count($url) - 1]);
                             $str = str_replace("-", " ", "$url");

                        if(strpos($_SERVER['REQUEST_URI'], '_cat') !== false){

                        echo "$str";
                    } else {
                        echo 'Products';
                    }

                ?>

        <?php endif; ?>

    </h2>

    <?php
        /**
         * woocommerce_before_main_content hook.
         *
         * @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
         * @hooked woocommerce_breadcrumb - 20
         * @hooked WC_Structured_Data::generate_website_data() - 30
         */
        do_action( 'woocommerce_before_main_content' );
    ?>

    <header class="woocommerce-products-header">

        <?php if ( apply_filters( 'woocommerce_show_page_title', true ) ) : ?>

            <!--<h1 class="woocommerce-products-header__title page-title ptbl"><?php woocommerce_page_title(); ?></h1>-->

        <?php endif; ?>

        <?php
            /**
             * woocommerce_archive_description hook.
             *
             * @hooked woocommerce_taxonomy_archive_description - 10
             * @hooked woocommerce_product_archive_description - 10
             */
            //do_action( 'woocommerce_archive_description' );
        ?>

    </header>






        <?php if ( have_posts() ) : ?>

            <?php woocommerce_product_loop_start(); ?>

                <div class="subcategories container">
                    <?php if ( woocommerce_product_subcategories() ): ?>

                    <?php

                              $taxonomyName =get_query_var( 'product_cat' ); // use use get_queried_object()->taxonomy; to get the current taxonomy name
                              $countchildren = count (get_term_children( $term_id, $taxonomyName ));

                     echo '<h3 class="group-products mts mbm  cat-title text-underline">'.'<span class="big">'.'Products Families'.'<span class="cont-post">'.'(' .$countchildren.')'.'</span>'.'</span>'.'</h3>';

                    ?>
                      <?php endif; ?>

                </div>
                    <?php
                /**
                 * woocommerce_before_shop_loop hook.
                 *
                 * @hooked wc_print_notices - 10
                 * @hooked woocommerce_result_count - 20
                 * @hooked woocommerce_catalog_ordering - 30
                 */
                do_action( 'woocommerce_before_shop_loop' );
            ?>




                    <div class="list-products container">
                        <?php $c=1; 
                        ?>

                <?php while ( have_posts() ) : the_post(); ?>


                    <?php
                        /**
                         * woocommerce_shop_loop hook.
                         *
                         * @hooked WC_Structured_Data::generate_product_data() - 10
                         */
                        do_action( 'woocommerce_shop_loop' );
                    ?>
                    <div class="col-sm-3">
                        <a class="product-image" data-fancybox data-type="iframe" data-src="<?php echo the_permalink( $product->id ); ?>" href="javascript:;" id="product_<?php echo $c; ?>">
                              <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
                              $thumb_url = $image[0];

                               $image_src = aq_resize( $thumb_url, 260, 209, true );    
                               ?>
                              <img src="<?php echo $image_src; ?>" />
                              <h3 class="woocommerce-loop-category__title"><?php the_title(); ?></h3>
                          </a>


                    <?php $c++; ?>

                    </div>
                <?php endwhile; // end of the loop. ?>
                </div>
            <?php woocommerce_product_loop_end(); ?>

            <?php
                /**
                 * woocommerce_after_shop_loop hook.
                 *
                 * @hooked woocommerce_pagination - 10
                 */
                do_action( 'woocommerce_after_shop_loop' );
            ?>

        <?php elseif ( ! woocommerce_product_subcategories( array( 'before' => woocommerce_product_loop_start( false ), 'after' => woocommerce_product_loop_end( false ) ) ) ) : ?>

            <?php
                /**
                 * woocommerce_no_products_found hook.
                 *
                 * @hooked wc_no_products_found - 10
                 */
                do_action( 'woocommerce_no_products_found' );
            ?>

        <?php endif; ?>

    <?php
        /**
         * woocommerce_after_main_content hook.
         *
         * @hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
         */
        do_action( 'woocommerce_after_main_content' );
    ?>



<?php get_footer( 'shop' ); ?>

And this is the part related to my problem :

<?php

                              $taxonomyName =get_query_var( 'product_cat' ); // use use get_queried_object()->taxonomy; to get the current taxonomy name
                              $countchildren = count (get_term_children( $term_id, $taxonomyName ));

                     echo '<h3 class="group-products mts mbm  cat-title text-underline">'.'<span class="big">'.'Products Families'.'<span class="cont-post">'.'(' .$countchildren.')'.'</span>'.'</span>'.'</h3>';

                    ?>   

Any help help would be appreciated.

  • 写回答

1条回答 默认 最新

  • duanliushua5026 2017-08-17 13:39
    关注

    You can simply use get_term_children() function to get the child category count this way:

    $countchildren = count( get_term_children( $queried_object->term_id, 'product_cat' ));
    

    Or

    $countchildren = count( get_term_children( $term_id, 'product_cat' ));
    

    Or

    $countchildren = count( get_term_children( get_queried_object()->term_id, get_queried_object()->taxonomy ));
    

    The 3 are working fine.

    But don't use:

    $taxonomyName =get_query_var( 'product_cat' );
    $countchildren = count (get_term_children( $term_id, $taxonomyName ));
    

    As $taxonomyName is in reality your main category slug, but not the taxonomy slug which is:

    $taxonomy = $queried_object->taxonomy;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab