好的,所以我有这些产品类别(即使它只是产品,它们实际上是类别):
$product_one = get_theme_mod('bakes_and_cakes_products_page_one');
$product_two = get_theme_mod('bakes_and_cakes_products_page_two');
$product_three = get_theme_mod('bakes_and_cakes_products_page_three');
$product_four = get_theme_mod('bakes_and_cakes_products_page_four');
$product_five = get_theme_mod('bakes_and_cakes_products_page_five');
我目前正在尝试使用我在网上找到的多个示例创建的代码:
if($product_one || $product_two || $product_three || $product_four || $product_five) {
$products_row1 = array( $product_one, $product_two, $product_three, $product_four, $product_five);
$products_row1 = array_diff( array_unique( $products_row1 ), array('') );
if($products_row1_qry->have_posts()){
$count = 0; // column counter
$rowSize = 4; // max columns per row
$terms = get_the_terms( 'product_cat', $products_row1);
foreach ( $terms as $term ){
$category_name = $term->name;
$category_thumbnail = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_url($category_thumbnail);
# columns display variables
$isStartOfNewRow = 0 === ($count % $rowSize); // modulo
$isEndOfRow = ($count>$rowSize-1 && $isStartOfNewRow);
$count++;
if ($isEndOfRow) { ?>
</div> <?php
}
if ($isStartOfNewRow) { ?>
<div class="row"> <?php
} ?>
<div class="col-4">
<div class="img-holder">
<img class="category-image" src="<?php echo $image; ?>">
</div>
<div class="text">
<h3> <?php echo $term ?> </h3>
<div class="btn-holder">
<a href="/product-category/<?php $term; ?> "> See the Offer!</a>
</div>
</div>
</div> <?php
}
$isStartOfNewRow = 0 === ($count % $rowSize);
if ($isStartOfNewRow) { ?>
</div> <?php
}
} ?>
</div> <?php
wp_reset_postdata();
}
它不起作用。我的目标是通过Wordpress主题定制器选择的那些特定产品类别显示在主页上,以便我们在每一行上最多显示四个产品类别。显然,这并不能很好地完成工作。我不太明白如何使用Woocommerce类别 - 我尝试使用WP_Query和一组常规WP页面(而不是Woocommerce的东西)做同样的事情,它工作。非常感谢任何帮助!