获取WooCommerce子分类产品

3
我正在尝试在主分类下显示带有产品的woocommerce子分类。
<ul class="wsubcategs">
<?php
$wsubargs = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0,
'parent' => $category->term_id,
'taxonomy' => 'product_cat'
);
$wsubcats = get_categories($wsubargs);
foreach ($wsubcats as $wsc):
?>
<li><a href="<?php echo get_term_link( $wsc->slug, $wsc->taxonomy );?>"><?php echo $wsc->name;?></a>

</li>
<?php
endforeach;
?>

</ul>
到目前为止,我已经按照正确的主分类得到了正确的子分类,但是它们下面没有显示任何产品。如有建议,请告知。 O.
2个回答

8
有许多方法可以在WordPress中实现这一点。您可以使用WP_Query对象执行自定义查询以获取该类别中的所有产品,这可能是最灵活的选项,但有更简单的方法。 WooCommerce提供了专门用于显示特定类别商品的短代码。输出将使用已内置在WooCommerce中的模板。
<?php echo do_shortcode('[product_category category="appliances"]');?>
这将为您提供特定类别下的产品。 在您的代码中,您可能会这样做:
<ul class="wsubcategs">
<?php
$wsubargs = array(
    'hierarchical' => 1,
    'show_option_none' => '',
    'hide_empty' => 0,
    'parent' => $category->term_id,
    'taxonomy' => 'product_cat'
);
$wsubcats = get_categories($wsubargs);
foreach ($wsubcats as $wsc):
?>
    <li>
        <a href="<?php echo get_term_link( $wsc->slug, $wsc->taxonomy );?>"><?php echo $wsc->name;?></a>
        <div class="products">
            <?php echo do_shortcode('[product_category category="'.$wsc->slug.'"]');?>
        </div>
    </li>
<?php endforeach;?>
</ul>
我注意到你的问题中有一个列表输出。我认为你可能不希望实际上在每个类别下面输出产品(详细模板),而是更倾向于显示子列表中的产品数量或产品标题。 以下是如何显示产品数量: 您可以在上述foreach循环的任何位置使用此代码。 以下是如何在每个类别的列表元素中显示产品标题子列表:
<?php $subcategory_products = new WP_Query( array( 'post_type' => 'product', 'product_cat' => $wsc->slug ) );
    if($subcategory_products->have_posts()):?>
    <ul class="subcat-products">
        <?php while ( $subcategory_products->have_posts() ) : $subcategory_products->the_post(); ?>
        <li>
            <a href="<?php echo get_permalink( $subcategory_products->post->ID ) ?>">
                <?php the_title(); ?>
            </a>
        </li>
        <?php endwhile;?>
    </ul>
<?php endif; wp_reset_query(); // Remember to reset ?>
以下是如何在您的代码中实现此功能:

这是在您上面的代码中实现此功能的方式:

<ul class="wsubcategs">
<?php
$wsubargs = array(
    'hierarchical' => 1,
    'show_option_none' => '',
    'hide_empty' => 0,
    'parent' => $category->term_id,
    'taxonomy' => 'product_cat'
);
$wsubcats = get_categories($wsubargs);
foreach ($wsubcats as $wsc):
?>
    <li>
        <a href="<?php echo get_term_link( $wsc->slug, $wsc->taxonomy );?>"><?php echo $wsc->name;?></a>
        <?php $subcategory_products = new WP_Query( array( 'post_type' => 'product', 'product_cat' => $wsc->slug ) );
            if($subcategory_products->have_posts()):?>
        <ul class="subcat-products">
            <?php while ( $subcategory_products->have_posts() ) : $subcategory_products->the_post(); ?>
            <li>
                <a href="<?php echo get_permalink( $subcategory_products->post->ID ) ?>">
                    <?php the_title(); ?>
                </a>
            </li>
            <?php endwhile;?>
        </ul>
    <?php endif; wp_reset_query(); // Remember to reset ?>
    </li>
<?php endforeach;?>
</ul>

0
**Show Category and Sub Category in search Box**


----------


<?php
//template name:house

?>
<style>
.abc {
    margin-left: 10px;
}
</style>

<?php
$taxonomy = 'product_cat';
$orderby = 'name';
$show_count = 0; // 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 = 0;

$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
?>
<select>
<?php
$all_categories = get_categories( $args );
foreach ($all_categories as $cat)
{

if($cat->category_parent == 0)
{
$category_id = $cat->term_id;
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );

$abc=$cat->name;
if($abc=="") {
?>
<option value="<?php echo $cat->slug;?>"><?php echo $abc; ?></option>

<?php }
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty

);

$sub_cats = get_categories( $args2 );
if($sub_cats)
{

foreach($sub_cats as $sub_category)
{
?>

<?php
if($sub_cats->$sub_category == 0)
{
$suv= $sub_category->cat_name;?>
<option class="abc" value="<?php echo $sub_category->slug; ?>"><span class="abc"><?php echo $suv; ?></span></option>




<?php } } } } } ?>

</select> 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,