categories - List subcategories of a specific product category (adapting from posts to products taxonomy)

I made the below for my sidebar to show the subcategories of a specific category (ID 89) of my choosing. Only the subcat

I made the below for my sidebar to show the subcategories of a specific category (ID 89) of my choosing. Only the subcategories that the post is in appear. Works great.

Now I want to change my posts in to WooCommerce Products, so I'm trying to adapt the code to target WC subcategories instead of post categories. I'm a bit lost on where to start.

<ul>
    <?php foreach (get_the_category() as $childcat) : ?>
    <?php if (cat_is_ancestor_of(89, $childcat)) { ?>
        <li>
            <a href="<?php echo get_category_link($childcat->term_id); ?>"><?php echo $childcat->cat_name; ?></a>
        </li>
    <?php } ?>
    <?php endforeach; ?>
</ul>

I made the below for my sidebar to show the subcategories of a specific category (ID 89) of my choosing. Only the subcategories that the post is in appear. Works great.

Now I want to change my posts in to WooCommerce Products, so I'm trying to adapt the code to target WC subcategories instead of post categories. I'm a bit lost on where to start.

<ul>
    <?php foreach (get_the_category() as $childcat) : ?>
    <?php if (cat_is_ancestor_of(89, $childcat)) { ?>
        <li>
            <a href="<?php echo get_category_link($childcat->term_id); ?>"><?php echo $childcat->cat_name; ?></a>
        </li>
    <?php } ?>
    <?php endforeach; ?>
</ul>
Share Improve this question asked Mar 17, 2019 at 19:05 SeanAUS120SeanAUS120 311 silver badge4 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

...to show the subcategories of a specific category (ID 89) ...

Try this

$product_cats = wp_get_post_terms( $post->ID, 'product_cat' );
if ( ! empty( $product_cats ) && ! is_wp_error( $product_cats ) ) { ?>
<ul>
    <?php 
    foreach ($product_cats as $childcat) : ?>
    <?php if ($childcat->parent == 89)) { ?>
        <li>
            <a href="<?php echo get_term_link($childcat->term_id); ?>"><?php echo $childcat->name; ?></a>
        </li>
    <?php } ?>
    <?php endforeach; ?>
</ul>
<?php } ?>

I hope this helps.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745645133a4637925.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信