获取woocommerce子类别产品 [英] Get woocommerce subcategory products

查看:47
本文介绍了获取woocommerce子类别产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将woocommerce子类别的产品显示在主要类别下。

I am trying to get the woocommerce subcategories with products to show under the main categories.

<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>

到目前为止,这已经使我在正确的主要类别下获得了正确的子类别,但没有显示任何产品在他们之下。
任何建议将不胜感激。

So far this has gotten me the corrent subcategories under the correct main categories, but no products are being shown under them. Any advice would be appreciated.

O。

推荐答案

在Wordpress中有很多方法可以做到这一点。您可以使用WP_Query对象进行自定义查询,以获取该类别中的所有产品,这是最灵活的选择,但是有一种更简单的方法。

There are many ways to do this in Wordpress. You could do a custom query using the WP_Query object to get all of the products in that category, which would be the most flexible option, but there is an easier way.

Woocommerce提供了专门用于显示特定类别产品的简码。输出将使用Woocommerce内置的模板。

Woocommerce provides shortcodes specifically for showing products in a specific category. The output would use the templates that are already built in to Woocommerce.

<?php echo do_shortcode('[product_category category="appliances"]');?>

这将为您提供特定类别的产品。

This will give you the products under a specific category.

在您的代码中,您可能会执行以下操作:

In your code, you might do something like this:

<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>

我在您的问题中注意到您正在输出列表。在我看来,您可能不想在每个类别下实际输出产品(详细模板),但您可能希望在子列表中显示产品或产品标题的数量。

I noticed in your question that you're outputting a list. It seems likely to me that you wouldn't want to actually output the products (detailed template) below each of the categories, but you might rather want to show the number of products or product titles in a sublist.

这是显示商品数量的方式:

Here's how you would show the number of products:

您可以在上面的foreach循环中的任何位置使用此代码。

You would use this anywhere in the foreach loop you have above.

以下是在每个类别的list元素中显示产品标题的子列表的方式:

Here's how you would show a sublist of titles of products in the list element for each category:

<?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 ?>

这就是上面代码中的样子:

And here's how that would look in your code above:

<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>

这篇关于获取woocommerce子类别产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆