admin管理员组文章数量:1122846
I am using WooCommerce in WordPress. I have few categories of products like these,
Example:
Product 1 Category (//parent)
-- Category 1
-- Category 2
-- Category 3
-- Category 4
Product 2 Category (//parent)
-- Category 10
-- Category 11
-- Category 12
-- Category 13
If I am in a archive page for Category 2(sub) How can I get all other category name under the parent category (Product 1 Category) as a list.
what i'm trying
$args = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0,
'parent' => $parent_cat_ID,
'taxonomy' => 'product_cat'
);
$subcats = get_categories($args);
echo '<ul class="wooc_sclist">';
foreach ($subcats as $sc) {
$link = get_term_link( $sc->slug, $sc->taxonomy );
echo '<li><a href="'. $link .'">'.$sc->name.'</a></li>';
}
echo '</ul>';
}
Out put should :
.Category 1
.Category 2
.Category 3
.Category 4
I have use below code which works fine in single-product.php page -
<?php
$parent = get_category_parents( $cat, true, ' » ' );
echo $product->get_categories( ', ', '<span>' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.</span>' );
?>
I am using WooCommerce in WordPress. I have few categories of products like these,
Example:
Product 1 Category (//parent)
-- Category 1
-- Category 2
-- Category 3
-- Category 4
Product 2 Category (//parent)
-- Category 10
-- Category 11
-- Category 12
-- Category 13
If I am in a archive page for Category 2(sub) How can I get all other category name under the parent category (Product 1 Category) as a list.
what i'm trying
$args = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0,
'parent' => $parent_cat_ID,
'taxonomy' => 'product_cat'
);
$subcats = get_categories($args);
echo '<ul class="wooc_sclist">';
foreach ($subcats as $sc) {
$link = get_term_link( $sc->slug, $sc->taxonomy );
echo '<li><a href="'. $link .'">'.$sc->name.'</a></li>';
}
echo '</ul>';
}
Out put should :
.Category 1
.Category 2
.Category 3
.Category 4
I have use below code which works fine in single-product.php page -
<?php
$parent = get_category_parents( $cat, true, ' » ' );
echo $product->get_categories( ', ', '<span>' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.</span>' );
?>
Share
Improve this question
edited Jul 30, 2015 at 4:50
Exclutips
asked Jul 29, 2015 at 22:38
ExclutipsExclutips
6851 gold badge6 silver badges15 bronze badges
2
|
2 Answers
Reset to default 0I usually do this by getting data from database
$result = mysql_query("SELECT * FROM wp_terms JOIN wp_term_taxonomy
ON wp_term_taxonomy.term_id = wp_terms.term_id
WHERE wp_term_taxonomy.parent = ".$parent_cat_ID."
AND wp_term_taxonomy.taxonomy = 'product_cat'");
I did this to solve the problem
<?php
$terms = get_the_terms( $post->cat_ID , 'product_cat' );
foreach ($terms as $term) {
$term_id = $term->term_id;
$term_link = get_term_link( $term, $taxonomy );
$term_name = $term->name;
echo '<a class="cat-box" href="' . $term_link . '"><span class="cat-name">' . $term_name . '</span></a>';
}
?>
本文标签: How to get related categorycategories in WordPress archive page
版权声明:本文标题:How to get related categorycategories in WordPress archive page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736289635a1928347.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
get_terms
. Woocommerce uses custom taxonomies, and not the build in taxonomycategory
– Pieter Goosen Commented Jul 30, 2015 at 4:21