![](/img/trans.png)
[英]Conditional WooCommerce email notification footer based on categories of products ordered
[英]WooCommerce products ordered by categories
我有WooCommerce属性存档页面,需要显示按类别排序的产品。 像这样:
等等
现在,我当前的自定义循环的结果是:
等等
因此,它基本上显示了每个类别中属性项内的所有产品,即使该产品不在此类别中也是如此。 我认为我正在忽略某些东西,但我不知道该怎么办?
<?php
/**
* Show products sorted by category
*/
$taxonomy = 'product_cat';
$orderby = 'menu_order';
$order = 'ASC';
$show_count = 1; // 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 = 1; // 1 for hide, 0 for show
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'order' => $order,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty,
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) :
$term_link = get_term_link( $cat );
if( $cat->category_parent == 0 ) :
$category_id = $cat->term_id;
$thumbnail_id = get_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
echo '<div id="' . $cat->slug . '" class="category ' . $cat->slug . '"><h2 class="category-title"><a href="'. esc_url( $term_link ) .'">'.$cat->name.'</a></h2>';
$loop = new WP_Query( $args );
while ( have_posts() ) : the_post();
global $product;
wc_get_template_part( 'content', 'product' );
endwhile; // end of the loop. ?>
</div><!-- end .category -->
<?php
wp_reset_query();
endif;
endforeach;
wp_reset_query();
?>
添加以下代码以获取类别明智的产品
<?php
/**
* Show products sorted by category
*/
$taxonomy = 'product_cat';
$orderby = 'menu_order';
$order = 'ASC';
$show_count = 1; // 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 = 1; // 1 for hide, 0 for show
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'order' => $order,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty,
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) :
$term_link = get_term_link( $cat );
if( $cat->category_parent == 0 ) :
$category_id = $cat->term_id;
$thumbnail_id = get_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
echo '<div id="' . $cat->slug . '" class="category ' . $cat->slug . '"><h2 class="category-title"><a href="'. esc_url( $term_link ) .'">'.$cat->name.'</a></h2>';
$product_args = array(
'type' => 'product',
'post_status' => 'publish',
'orderby' => 'name',
'order' => 'DESC',
'pad_counts' => false,
'hierarchical' => 1,
'hide_empty' => 0,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cat->term_id
)
),
);
$loop = new WP_Query( $product_args );
while ( have_posts() ) : the_post();
global $product;
wc_get_template_part( 'content', 'product' );
endwhile; // end of the loop. ?>
</div><!-- end .category -->
<?php
wp_reset_query();
endif;
endforeach;
wp_reset_query();
?>
get_categories()和WP_Query()的参数不同。
关于明智地获取产品类别的争论。
$args_for_product = array(
'tax_query' => array(array('taxonomy' =>$taxonomy,'field' => 'slug','terms' =>$cat->slug)),
'orderby' => $orderby,
'order' => $order,
);
使用tax_query
传递产品查询中的类别。 show_count,pad_counts,hierarchical,title_li,hide_empty
其他参数( 例如show_count,pad_counts,hierarchical,title_li,hide_empty
不通过WP_Query传递 )。 将上面的代码放在类别循环中
同时在while
循环have_post()
更改为$loop->have_post()
,将the_post()
更改$loop->the_post()
完整的代码
<?php
/**
* Show products sorted by category
*/
$taxonomy = 'product_cat';
$orderby = 'menu_order';
$order = 'ASC';
$show_count = 1; // 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 = 1; // 1 for hide, 0 for show
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'order' => $order,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty,
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) :
$term_link = get_term_link( $cat );
if( $cat->category_parent == 0 ) :
$category_id = $cat->term_id;
$thumbnail_id = get_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
echo '<div id="' . $cat->slug . '" class="category ' . $cat->slug . '"><h2 class="category-title"><a href="'. esc_url( $term_link ) .'">'.$cat->name.'</a></h2>';
$args_for_product = array('type'=> 'product',
'post_status' => 'publish',
'tax_query' => array(array('taxonomy' =>$taxonomy,'field' => 'slug','terms' =>$cat->slug)),
'orderby' => $orderby,
'order' => $order,
);
$loop = new WP_Query( $args_for_product );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
wc_get_template_part( 'content', 'product' );
endwhile; // end of the loop. ?>
</div><!-- end .category -->
<?php
wp_reset_query();
endif;
endforeach;
wp_reset_query();
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.