我正在使用WP-Ecommerce,并希望在单个产品页面上显示该产品的类别。我已经四处寻找并发现了一些东西(Rohan在这个网站上发布)几乎可以工作:
function cdl_get_cat() {
global $wp_query, $wpsc_query;
$query_data = Array();
$cdl_post_id = wpsc_the_product_id();
$categories = wp_get_object_terms( $cdl_post_id , 'wpsc_product_category' );
//if product is associated w more than one category
if(count($categories) > 1 && isset($wpsc_query->query_vars['wpsc_product_category']))
$query_data['category'] = $wpsc_query->query_vars['wpsc_product_category'];
elseif(count($categories) > 0)
$query_data['category'] = $categories[0]->slug;
return $query_data['category'];
}
echo cdl_get_cat();
虽然这段代码可以满足我的需要,但它会将类别名称显示为一个slug(即眼影而不是Eye-shadow)。我不是很熟悉PHP,所以我不确定需要更改什么才能显示名称而不是slug。
答案 0 :(得分:1)
我一整天都在苦苦挣扎,我也不擅长PHP ......但我已经通过删除这一点(我不理解......)来实现它。:
//if product is associated w more than one category
if(count($categories) > 1 && isset($wpsc_query->query_vars['wpsc_product_category']))
$query_data['category'] = $wpsc_query->query_vars['wpsc_product_category'];
elseif(count($categories) > 0)
然后在这里将'slug'改为'name':
$query_data['category'] = $categories[0]->slug;
所以你需要的就是:
<?php function cdl_get_cat() {
global $wp_query, $wpsc_query;
$query_data = Array();
$cdl_post_id = wpsc_the_product_id();
$categories = wp_get_object_terms( $cdl_post_id , 'wpsc_product_category' );
$query_data['category'] = $categories[0]->name;
return $query_data['category'];
}
echo cdl_get_cat();
?>
如果你有一个产品的更多一个类别更改[0]会给你一个不同的,因为我认为$ categories是一个包含所有相关类别的数组。如果需要,您应该可以遍历所有这些循环。我应该注意如何做到这一点!
答案 1 :(得分:1)
谢谢,发现这对于将类添加到其所属类别的产品非常有用:
在产品循环之前:
<?php function cdl_get_cat() {
global $wp_query, $wpsc_query;
$query_data = Array();
$cdl_post_id = wpsc_the_product_id();
$categories = wp_get_object_terms( $cdl_post_id , 'wpsc_product_category' );
$count = count($categories);
for ($i = 0; $i < ($count); $i++) {
$query_data['category'] = $categories[$i]->name;
echo $query_data['category'];
if ($i < ($count - 1))
echo ", ";
}
}
cdl_get_cat();
?>
然后在循环中:
<li class="product_grid_item product_view_<?php echo wpsc_the_product_id(); ?> <?php cdl_get_cat(); ?>">
答案 2 :(得分:0)
<?php function cdl_get_cat() {
global $wp_query, $wpsc_query;
$query_data = Array();
$cdl_post_id = wpsc_the_product_id();
$categories = wp_get_object_terms( $cdl_post_id , 'wpsc_product_category' );
$count = count($categories);
for ($i = 0; $i < ($count); $i++) {
$query_data['category'] = $categories[$i]->name;
echo $query_data['category'];
if ($i < ($count - 1))
echo ", ";
}
}
cdl_get_cat();
?>