将 Woocommerce 类别 ID 转换为 slug 函数
Turn Woocommerce category ID into slug function
我正在寻找一个函数,将 woocommerce 类别 ID 转换为面包屑的类别 slug link。
以下循环提取产品页面最上面的产品类别 ID。这是必要的,因为我的一些产品有 4 或 5 个层次结构。
这工作正常。
$prod_terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($prod_terms as $prod_term) {
// gets product cat id
$product_cat_id = $prod_term->term_id;
// gets an array of all parent category levels
$product_parent_categories_all_hierachy = get_ancestors( $product_cat_id, 'product_cat' );
// This cuts the array and extracts the last set in the array
$last_parent_cat = array_slice($product_parent_categories_all_hierachy, -1, 1, true);
foreach($last_parent_cat as $last_parent_cat_value){
}
}
此时echo $last_parent_cat_value;
会给你最高类别的ID。
为了将其变成面包屑 link 我需要提取类别名称。所以...
// This function turns Category Ids produced by the foreach loop above into Names
function woocommerceCategoryName($id){
$term = get_term( $id, 'product_cat' );
return $term->name;
}
现在我所需要的只是完成 link 的子弹。所以...
//this function gets the category slug from the category id
function get_cat_slug($cat_id) {
$cat_id = (int) $cat_id;
$category = &get_category($cat_id);
return $category->slug;
}
但是当我echo
echo '<a href="' . home_url() . '/product-category/' . get_cat_slug($last_parent_cat_value) . '/">' . woocommerceCategoryName($last_parent_cat_value) . '</a>';
我的命名功能可以使用,但我的 slug 功能不能使用。
任何人都可以指出我的错误吗?
当我在前端 inspect element
这里是我生成的 html...
<a href="http://home.com/product-category//">Sports</a>
我可以尝试使用名称生成 slug 吗?
感谢阅读。
好的,我不确定为什么上面的方法不起作用,或者为什么下面的方法起作用。
//I copied the function to generate the name, and generated a slug instead...
function woocommerceCategorySlug($id){
$term = get_term( $id, 'product_cat' );
return $term->slug;
}
echo '<a href="' . home_url() . '/product-category/' . woocommerceCategorySlug($last_parent_cat_value) . '/">' . woocommerceCategoryName($last_parent_cat_value) . '</a>';
再次感谢您的阅读,希望这对其他人有所帮助。
我正在寻找一个函数,将 woocommerce 类别 ID 转换为面包屑的类别 slug link。
以下循环提取产品页面最上面的产品类别 ID。这是必要的,因为我的一些产品有 4 或 5 个层次结构。
这工作正常。
$prod_terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($prod_terms as $prod_term) {
// gets product cat id
$product_cat_id = $prod_term->term_id;
// gets an array of all parent category levels
$product_parent_categories_all_hierachy = get_ancestors( $product_cat_id, 'product_cat' );
// This cuts the array and extracts the last set in the array
$last_parent_cat = array_slice($product_parent_categories_all_hierachy, -1, 1, true);
foreach($last_parent_cat as $last_parent_cat_value){
}
}
此时echo $last_parent_cat_value;
会给你最高类别的ID。
为了将其变成面包屑 link 我需要提取类别名称。所以...
// This function turns Category Ids produced by the foreach loop above into Names
function woocommerceCategoryName($id){
$term = get_term( $id, 'product_cat' );
return $term->name;
}
现在我所需要的只是完成 link 的子弹。所以...
//this function gets the category slug from the category id
function get_cat_slug($cat_id) {
$cat_id = (int) $cat_id;
$category = &get_category($cat_id);
return $category->slug;
}
但是当我echo
echo '<a href="' . home_url() . '/product-category/' . get_cat_slug($last_parent_cat_value) . '/">' . woocommerceCategoryName($last_parent_cat_value) . '</a>';
我的命名功能可以使用,但我的 slug 功能不能使用。
任何人都可以指出我的错误吗?
当我在前端 inspect element
这里是我生成的 html...
<a href="http://home.com/product-category//">Sports</a>
我可以尝试使用名称生成 slug 吗?
感谢阅读。
好的,我不确定为什么上面的方法不起作用,或者为什么下面的方法起作用。
//I copied the function to generate the name, and generated a slug instead...
function woocommerceCategorySlug($id){
$term = get_term( $id, 'product_cat' );
return $term->slug;
}
echo '<a href="' . home_url() . '/product-category/' . woocommerceCategorySlug($last_parent_cat_value) . '/">' . woocommerceCategoryName($last_parent_cat_value) . '</a>';
再次感谢您的阅读,希望这对其他人有所帮助。