可以使用get\\u terms()和循环获取产品类别。
<?php
$cat_args = array( \'orderby\' => \'name\', \'order\' => \'asc\', \'hide_empty\' => true);
$cats = get_terms( \'product_cat\', $cat_args );
foreach ($cats as $key => $cat):
if ($cat->parent == 0): ?>
<a href="<?php echo get_term_link($cat) ?>"><?php echo $cat->name; ?></a> <?php
endif;
endforeach;
这将返回类别(“第一代”),为了拥有子类别,我们只需要在现有的循环中添加一个新的循环。下一个代码一直持续到第三代。
<?php
$cat_args = array( \'orderby\' => \'name\', \'order\' => \'asc\', \'hide_empty\' => true);
$cats = get_terms( \'product_cat\', $cat_args );
foreach ($cats as $key => $cat):
if ($cat->parent == 0): ?>
<a href="<?php echo get_term_link($cat) ?>"><?php echo $cat->name; ?></a> <?php
foreach ($cats as $key => $cat2):
if ($cat2->parent == $cat->term_id): ?>
<a href="<?php echo get_term_link($cat2) ?>"><?php echo $cat2->name; ?></a> <?php
foreach ($cats as $key => $cat3):
if ($cat3->parent == $cat2->term_id): ?>
<a href="<?php echo get_term_link($cat3) ?>"><?php echo $cat3->name; ?></a><?php
endif;
endforeach;
endif;
endforeach;
endif;
endforeach;
这只显示包含产品的类别,所有类别只需将第一行的“hide\\u empty”更改为false。
要显示类别或子类别的产品名称,我们可以使用此功能。
<?php
function get_prod($prod_per_page, $prod_slug){
$args = array( \'posts_per_page\' => $prod_per_page,
\'tax_query\' => array(
\'relation\' => \'AND\',
array(\'taxonomy\' => \'product_cat\',
\'field\' => \'slug\',
\'terms\' => $prod_slug
)
),
\'post_type\' => \'product\',
\'orderby\' => \'title\'
);
$products = new WP_Query( $args );
echo "<ul>";
while ( $products->have_posts() ) {
$products->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li> <?php
}
wp_reset_postdata();
echo "</ul>";
}
现在,我们可以在a标签旁边添加此自定义函数,该函数有2个参数,首先是要显示的产品数量,其中-1是全部,例如,对于4个产品,是4,第二个是类别slug。
这里是答案,现在您可以删除第3个“;“生成”;如果不需要,请更改类别,并更改每页显示的产品数量。
<?php
function get_prod($prod_per_page, $prod_slug){
$args = array( \'posts_per_page\' => $prod_per_page,
\'tax_query\' => array(
\'relation\' => \'AND\',
array(\'taxonomy\' => \'product_cat\',
\'field\' => \'slug\',
\'terms\' => $prod_slug
)
),
\'post_type\' => \'product\',
\'orderby\' => \'title\'
);
$products = new WP_Query( $args );
echo "<ul>";
while ( $products->have_posts() ) {
$products->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li> <?php
}
wp_reset_postdata();
echo "</ul>";
}
$cat_args = array( \'orderby\' => \'name\', \'order\' => \'asc\', \'hide_empty\' => true);
$cats = get_terms( \'product_cat\', $cat_args );
foreach ($cats as $key => $cat):
if ($cat->parent == 0): ?>
<a href="<?php echo get_term_link($cat) ?>"><?php echo $cat->name; ?></a> <?php
get_prod(-1, $cat->slug);
foreach ($cats as $key => $cat2):
if ($cat2->parent == $cat->term_id): ?>
<a href="<?php echo get_term_link($cat2) ?>"><?php echo $cat2->name; ?></a> <?php
get_prod(10, $cat2->slug);
foreach ($cats as $key => $cat3):
if ($cat3->parent == $cat2->term_id): ?>
<a href="<?php echo get_term_link($cat3) ?>"><?php echo $cat3->name; ?></a><?php
get_prod(4, $cat3->slug);
endif;
endforeach;
endif;
endforeach;
endif;
endforeach;
这个get\\u prod()的第一个参数是更改每个人想要显示的产品数量。
可以添加html、css或javascript!希望对您有所帮助!