これは私がすべての商品カテゴリーをサムネイルで表示するために使うコードです:
<?php
$cats = get_terms('product_cat', array(
'hide_empty' => 0,
'orderby' => 'name'
));
?>
<div class="container">
<?php foreach($cats as $cat) : ?>
<?php global $wp_query;
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
echo '<li><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a>';
echo '<img src="'.$image.'" width="45px" height="45px" />';
}
?>
<?php endforeach; ?>
</div>
サムネイルだけのサブカテゴリを持ちながら、誰でも私にどのようにサブカテゴリのカテゴリを分割するのを手伝ってもらえますか。このような:
カテゴリ(テキスト)
カテゴリ(テキスト)
私はこれを作る方法を見つけました:
<?php
$taxonomy = 'product_cat';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
?>
<?php $all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {?>
<?php $category_id = $cat->term_id;
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
echo '<h3 style="font-size:16px">'. $cat->name .'</h3>';
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories( $args2 );
if($sub_cats) {
foreach($sub_cats as $sub_category) {
if($sub_cats->$sub_category == 0) {
$thumbnail_id = get_woocommerce_term_meta( $sub_category->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
echo '<li style="list-style-type: none"><a href="'. get_term_link($sub_category->slug, 'product_cat') .'"><img src="'.$image.'" width="45px" height="45px" style="margin-right:8px; margin-top:8px; float:left; " /></a></li>';
}
}
echo '</li>';
}?>
<?php }
} ?>