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>';
?>
任意のヘルプのヘルプをいただければ幸いです。あなたは、単にこの方法を数える子カテゴリを取得するにはget_term_children()
機能を使用することができます
ありがとうございました。それは絶対に働いた –
こんにちは、
これまで私を助けていただきありがとうございます、ショップのページで親カテゴリの数を数えたい場合はどうしたらいいですか? –