2017-08-17 39 views
2

我已編輯woocommerce archive-product.php文件以實現我的佈局,但仍有一件事需要弄清楚。我想顯示每個頁面中存檔的現有子類別的總數。獲取每個WooCommerce父類別的現有子類別的總數

例如我有兩個主要產品類別(A,B)在它們中的每一個具有一定的亞類:

Category A 
    Subcategory A1 
    Subcategory A2 

Category B 
    Subcategory B1 
    Subcategory B2 
    Subcategory B3 

我要顯示的每個父的子類別的,在我的情況2 A歸檔頁面中的子類別和B歸檔頁面中的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>'; 

        ?> 

任何幫助幫助將不勝感激。

回答

1

您可以簡單地使用get_term_children()功能來獲取子類別數是這樣的:

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

或者

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

或者

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

三是工作的罰款。

但不要用:

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

由於$taxonomyName在現實中是你的主要類別塞,而不是分類蛞蝓是:

$taxonomy = $queried_object->taxonomy; 
+0

非常感謝您的幫助。它絕對有效 –

+1

嗨Loic,
謝謝你幫助我到目前爲止,如果我要計算店鋪頁面中父類別的數量? –

相關問題