Woocommerce类别循环仅显示1个类别

时间:2019-06-19 14:56:28

标签: loops woocommerce categories

我确定我做错了什么,只是看不到它。

我修改了Woocommerce content-product_cat.php文件的布局(并将其放在主题的本地Woocommerce文件夹中),我将代码更改为以下内容:

<?php
/**
 * The template for displaying product category thumbnails within loops
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/content-product_cat.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/
 * @package WooCommerce/Templates
 * @version 2.6.1
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
?>
<?php
$terms = get_the_terms( $post->ID, 'product_cat' );
    foreach ( $terms as $term ){
		$category_link = get_category_link($term);
        $category_name = $term->name;
        $category_thumbnail = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
        $backgroundImg = wp_get_attachment_url($category_thumbnail);
    }
?>

<div class="boxitem col-xl-3 col-lg-6 col-md-6 col-sm-12 col-xs-12 item">
	<div class="boxcontainer">
		<div class="boximage" style="background: url('<?php echo $backgroundImg; ?>') no-repeat;"></div>
		<div class="textcontainer">
		<h2><?php echo $category_name; ?></h2>
		<?php echo category_description( $term ); ?>
		</div>
		<div class="boxbutton"><a href="<?php echo esc_url( $category_link ); ?>" class="pinkbutton">More info</a></div>
	</div>
</div>

我也略微更改了archive-product.php(以与主题文件夹中Woocommerce文件夹中的上述方法相同的方式来覆盖Woocommerce的基本布局),以包括标题和节控制器,如下所示:

<section class="page woocommerce">
<div class="container pagecontent titlecontainer">
<div class="page_title"><h1><?php woocommerce_page_title(); ?></h1></div>
<div class="site-breadcrumbs"><a href="<?php echo esc_url( home_url( '/' ) ); ?>shop/" class="pinkbutton">&lsaquo; Back to Shop</a></div>
</div>

<div class="container pagecontent contentcontainer">
<div class="page_content">
 <div class="post-tags">
			<?php 
    $args = array(
        'taxonomy' => 'product_cat',
        'orderby' => 'name',
        'hierarchical' => 0, // 1 for yes, 0 for no  
        'hide_empty' => 0,
    );
    $all_categories = get_categories($args);
 foreach ( $all_categories as $cat ) {  //for each term:
	$class = ( is_category( $cat->slug ) ) ? 'active' : '';
	echo "<a href='".get_term_link($cat)."' class='" . $class . "'>" . $cat->name . "</a>\n";
 }
 ?>

</div>
<div class="boxes tagged-posts filtered-posts ">

这只是在子类别页面上添加了过滤器,而在主类别页面上时我有CSS将其隐藏。

我的问题是,在主要类别上,或者当您逛商店时,我设置它来显示类别,这很好,它确实可以,但是它会重复显示相同的类别。如下图所示:

error repeating category image

我的问题是:

有人可以检查我的语法并让我知道我哪里出错了吗?因为我确定我只是错过了一些东西,或者在某个地方添加了一些东西,但我不应该在那儿,现在类别显示不正确。

任何帮助都会很棒!

0 个答案:

没有答案