Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获取每个父类别的现有子类别总数_Php_Wordpress_Woocommerce_Categories_Product - Fatal编程技术网

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

Php 获取每个父类别的现有子类别总数,php,wordpress,woocommerce,categories,product,Php,Wordpress,Woocommerce,Categories,Product,我已经编辑了woocommerce archive-product.php文件来实现我的布局,但还有一件事我需要弄清楚。我想显示存档每页中现有子类别的总数 例如,我有两个主要产品类别(A、B),每个类别都有一些子类别: A类 子类别A1 子类别A2 B类 子类别B1 子类别B2 子类别B3 我想显示每个父项的子类别的数量,在我的例子中,在存档页面中显示2个子类别,在存档页面中显示3个子类别 这就是我到目前为止所做的: <?php /** * The Template for d

我已经编辑了woocommerce archive-product.php文件来实现我的布局,但还有一件事我需要弄清楚。我想显示存档每页中现有子类别的总数

例如,我有两个主要产品类别(A、B),每个类别都有一些子类别:

A类
子类别A1
子类别A2
B类
子类别B1
子类别B2
子类别B3
我想显示每个父项的子类别的数量,在我的例子中,在存档页面中显示2个子类别,在存档页面中显示3个子类别

这就是我到目前为止所做的:

    <?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' ); ?>

这是与我的问题相关的部分:

<?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>';

                    ?>   

任何帮助都将不胜感激。

您只需使用函数以这种方式获取子类别计数:

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

这三个工作正常

但不要使用:

$taxonomyName =get_query_var( 'product_cat' );
$countchildren = count (get_term_children( $term_id, $taxonomyName ));
正如
$taxonomyName
实际上是您的主要类别slug,但不是分类slug,它是:

$taxonomy = $queried_object->taxonomy;

非常感谢你的帮助。这绝对有效。Hi Loic,
感谢您迄今为止对我的帮助,如果我想计算商店页面中父类别的数量,该怎么办?
$taxonomyName =get_query_var( 'product_cat' );
$countchildren = count (get_term_children( $term_id, $taxonomyName ));
$taxonomy = $queried_object->taxonomy;