我正在使用csv文件进行产品导入。我需要首先从csv显示所有产品的列表,然后需要显示来自woocommerce的所有类别。所有类别和子类别即将到来,但我需要在多重检查选择框中按层次顺序显示它们,例如
Clothes
-- Jeans
------ Blue
------ Black
-- T-shirt
Jwellery
-- Diamond
------ Chain
------ Ring
-- Gold
Phone
但所有这些都是单线的。我无法找到哪个是父类别。这是我的代码。请帮助。
$args = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0,
'taxonomy' => 'product_cat'
);
$subcats = get_categories($args);
echo '<ul id="test" class="test">';
foreach ($subcats as $sc) {
$link = get_term_link( $sc->slug, $sc->taxonomy );
if($sc->category_parent == 0)
{
echo '<li><input type="checkbox" name="product_cat[]" value="'.$sc->name.'"/><a href="'. $link .'">'.$sc->name.'</a></li>';
}else
{
echo '<li> <input type="checkbox" name="product_cat[]" value="'.$sc->name.'"/><a href="'. $link .'">'.$sc->name.'</a></li>';
}
}
echo '</ul>';
答案 0 :(得分:0)
试试这个代码兄弟
$args = array(
'taxonomy' => 'product_cat',
'parent' => false, // get top level categories
'orderby' => 'name',
//'order' => 'ASC',
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => false,
);
$categories = get_categories( $args );
echo "<ul>";
foreach ( $categories as $category ){
$link=get_category_link( $category->term_id );
echo "<label><a href=".$link."><input type=checkbox value=". $category->name . " name=". $category->name . ">". $category->name ."</a>" ."</label>";
$sub_args = array(
'taxonomy' => 'product_cat',
'parent' => $category->term_id, // get child categories
'orderby' => 'name',
//'order' => 'ASC',
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => false,
);
$sub_categories = get_categories( $sub_args );
?>
<ul>
<?php
foreach ( $sub_categories as $sub_category ){
$links=get_category_link( $sub_category->term_id );
?>
<li> <?php
echo '<label><a href='.$links.'><input type="checkbox" id="type-'. $sub_category->name . '" rel="'. $sub_category->name . '">'. $sub_category->name .'</a>'. '</label>';
?>
</li>
<?php
////////////////////////
$sub_arg = array(
'taxonomy' => 'product_cat',
'child_of' => $sub_category->term_id, // get child categories
'orderby' => 'name',
//'order' => 'ASC',
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => false,
);
$su_categories = get_categories( $sub_arg );
?>
<ul>
<?php
foreach ( $su_categories as $su_category ){
$cat_link=get_category_link( $su_category->term_id );
?>
<li> <?php
echo '<label><a href='.$links.'><input type="checkbox" id="type-'. $su_category->name . '" rel="'. $su_category->name . '">'. $su_category->name .'</a>'. '</label>';
?>
</li>
<?php
}
//////////////////////////////
}
?>
</ul>
<?php
}
echo "</ul>";
答案 1 :(得分:0)
Use below code
$taxonomy = 'product_cat';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
echo '<br /><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a>';
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories( $args2 );
if($sub_cats) {
foreach($sub_cats as $sub_category) {
echo $sub_category->name ;
}
}
}
}
答案 2 :(得分:0)
在您的位置使用此短代码:-[woocommerce_nested_categories1]
将此代码添加到您的function.php中:-
function woocommerce_nested_categories() {
$taxonomy = 'product_cat';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$all_categories = get_categories( $args );
echo'<div class="outer-nested-woo-catg"><ul>';
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
echo '<br /><li><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a></li>';
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories( $args2 );
if($sub_cats) {
echo'<ul>';
foreach($sub_cats as $sub_category) {
echo '<li><a href="'.get_term_link($sub_category->slug,'product_cat').'">'.$sub_category->name.'</a></li>' ;
$args3 = array(
'taxonomy' => $taxonomy,
'child_of' => 1,
'parent' => $sub_category->term_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats2= get_categories( $args3 );
if($sub_cats2){
echo'<ul>';
foreach($sub_cats2 as $sub_category2) {
echo '<li><a href="'.get_term_link($sub_category2->slug,'product_cat').'">'.$sub_category2->name.'</a></li>' ;
$args4 = array(
'taxonomy' => $taxonomy,
'child_of' => 1,
'parent' => $sub_category2->term_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats3= get_categories( $args4 );
if($sub_cats3){
echo'<ul>';
foreach($sub_cats3 as $sub_category3) {
echo '<li><a href="'.get_term_link($sub_category3->slug,'product_cat').'">'.$sub_category3->name.'</a></li>' ;
$args5 = array(
'taxonomy' => $taxonomy,
'child_of' => 1,
'parent' => $sub_category3->term_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats4= get_categories( $args5 );
if($sub_cats4) {
echo'<ul>';
foreach($sub_cats4 as $sub_category4) {
echo '<li><a href="'.get_term_link($sub_category4->slug,'product_cat').'">'.$sub_category4->name.'</a></li>' ;
}
echo'</ul>';
}
}
echo'</ul>';
}
}
echo'</ul>';
}
}
echo'</ul>';
}
}
}
echo'</ul></div>';
}
add_shortcode('woocommerce_nested_categories1', 'woocommerce_nested_categories');