Php 如何在WordPress中按类别对woocommerce产品进行分组
我至少有4个父类别,每个父类别都有一个子类别 WordPress中的类别如下所示:Php 如何在WordPress中按类别对woocommerce产品进行分组,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我至少有4个父类别,每个父类别都有一个子类别 WordPress中的类别如下所示: $my_categories = get_terms( 'TERM_NAME_HERE' ); $my_categories_count = count( $my_categories ); if ( $my_categories_count > 0 && is_array( $my_categories ) ) { echo '<div class="wrap">';
$my_categories = get_terms( 'TERM_NAME_HERE' );
$my_categories_count = count( $my_categories );
if ( $my_categories_count > 0 && is_array( $my_categories ) ) {
echo '<div class="wrap">';
foreach ( $my_categories as $single_cat ) { ?>
<h2><?php echo $single_cat->name; ?></h2>
<?php
$cat_posts_args = array(
'post_type' => 'product',
'order' => 'ASC',
'orderby' => 'date',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $single_cat->term_id,
'include_children' => false
)
)
);
$cat_posts = new WP_Query( $cat_posts_args );
if ( $cat_posts->have_posts() ) :
echo '<p>';
while ( $cat_posts->have_posts() ) : $cat_posts->the_post(); ?>
<a href="<?php the_permalink(); ?>"><span><?php the_title(); ?></span>: <?php echo get_the_excerpt(); ?></a><br>
<?php endwhile;
echo '</p>';
else :
if ( !$parent ) echo '<p>No products found.</p>';
endif;
wp_reset_postdata();
} // end foreach
echo '</div>';
}
- Lidingö(父类)
- Direktåtkomst Förrådslänga(子类别)
- 7千伏安(产品)
- 6KVM(产品)
- 入口平面图(子类别)
- 1kbm(产品)
- 1.5千伏安(产品)
- Direktåtkomst Förrådslänga(子类别)
- Nacka(父类)
- 样本(子类别)
- 机管局(产品)
- bbb(产品)
- 样本(子类别)
<?php
$args = array(
'post_type' => 'product',
array(
'taxonomy' => 'product_cat'
),
'posts_per_page' => 6,
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
echo woocommerce_template_single_title();
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
我认为上面的代码不正确,因为每个分页都显示相同的产品
您知道按类别对所有woocommerce产品进行分组的正确查询是什么吗?谢谢您的查询要求所有属于产品类别分类法的产品,即所有产品 您需要通过将要搜索的类别添加到参数来缩小搜索范围。例如,如果要通过类别slug进行搜索,可以使用:
$args = array(
'post_type' => 'product',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'category-slug1'
),
'posts_per_page' => 6,
);
要循环浏览一个页面上的所有类别,您需要以下内容:
$my_categories = get_terms( 'TERM_NAME_HERE' );
$my_categories_count = count( $my_categories );
if ( $my_categories_count > 0 && is_array( $my_categories ) ) {
echo '<div class="wrap">';
foreach ( $my_categories as $single_cat ) { ?>
<h2><?php echo $single_cat->name; ?></h2>
<?php
$cat_posts_args = array(
'post_type' => 'product',
'order' => 'ASC',
'orderby' => 'date',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $single_cat->term_id,
'include_children' => false
)
)
);
$cat_posts = new WP_Query( $cat_posts_args );
if ( $cat_posts->have_posts() ) :
echo '<p>';
while ( $cat_posts->have_posts() ) : $cat_posts->the_post(); ?>
<a href="<?php the_permalink(); ?>"><span><?php the_title(); ?></span>: <?php echo get_the_excerpt(); ?></a><br>
<?php endwhile;
echo '</p>';
else :
if ( !$parent ) echo '<p>No products found.</p>';
endif;
wp_reset_postdata();
} // end foreach
echo '</div>';
}
$my_categories=get_terms('TERM_NAME_HERE');
$my\u categories\u count=计数($my\u categories);
if($my\u categories\u count>0&&is\u数组($my\u categories)){
回声';
foreach($single\u cat作为我的分类){?>
首先,您需要将此woocommerce模板覆盖到当前主题,因为您必须使用自定义循环按类别获取产品
只需将woocommerce的模板复制到您当前的主题->创建文件夹名称(woocommerce)->将模板粘贴到该文件夹即可
使用以下代码:
$parent_terms= get_terms( array( 'taxonomy' => 'product_cat', 'parent' => 0 ) );
if($parent_terms= )
{
foreach( $parent_terms as $parent_term )
{
$child_terms = get_terms( array( 'taxonomy' => 'product_cat', 'parent' => $parent_term->term_id ) );
if($child_terms)
{
foreach( $child_terms as $child_term )
{
$product_args=
array(
'posts_per_page' => 50,
'post_type' => 'my_custom_type',
'posts_per_page' => 6,
'cat' => $child_term->term_id
);
$product_query = new WP_Query( $product_args );
while($product_query->have_posts()) : $product_query->the_post();
{
Here you will get product detail so you can display product details as you wanted
}
}
}
}
}
你能给你的网站一个链接吗?预期的输出是什么?