gpt4 book ai didi

php - 如何在 Woocommerce 的产品页面中获取当前类别 ID?

转载 作者:行者123 更新时间:2023-12-05 08:19:03 26 4
gpt4 key购买 nike

我想获取产品页面中的当前类别 ID,以便在 templates/single-product/add-to-cart/simple.phpin Woocommerce

中显示内容
If the category ID == 1 {//i'll show something}

if the category ID == 2 {//something else}

所有这些都在“simple.php

谢谢

编辑:

<?php echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', $cat_count, 'woocommerce' ) . ' ', '</span>' ); ?>

这段代码给出了当前产品的产品名称,如果我不能得到ID,我怎么能得到类别名称的字符串呢?

最佳答案

使用此代码

global $wp_query;
$terms_post = get_the_terms( $post->cat_ID , 'product_cat' );
foreach ($terms_post as $term_cat) {
$term_cat_id = $term_cat->term_id;
echo $term_cat_id;
}

关于php - 如何在 Woocommerce 的产品页面中获取当前类别 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33833880/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com