通过子类别获取woocommerce类别

时间:2017-04-18 06:25:46

标签: php wordpress woocommerce

我想让所有的woocommerce类别在前端与子类别一样,如下结果:

<ul>
    <li><a href="">Link</a>
        <ul>
            <li><a href="">Submenu link</a></li>
        </ul>
    </li>
</ul>

这就是我所拥有的(但它不是我想要的):

<?php

  $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 '<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) {
                foreach($sub_cats as $sub_category) {
                    echo  '<li><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a></li>';
                }
            }
        }       
}
?>  

此代码显示的类别和子类别,但子类别不应该在哪里,子类别就像这样的单独链接:

<ul>
    <li><a href="">link</a></li>
    <li><a href="">Submenu link</a></li>
</ul>

5 个答案:

答案 0 :(得分:4)

您可以尝试以下代码:

<TextBlock x:Name="TheTextBlock" DataContext="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Text" Value="{Binding ToggleTrueText}" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsToggled}" Value="False">
                    <Setter Property="Text" Value="{Binding ToggleFalseText}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

这将在您的WooCommerce,基于Wordpress的网站上打印。

答案 1 :(得分:1)

$taxonomy     = 'product_cat';
$orderby      = 'name';  
$show_count   = 0;      
$pad_counts   = 0;      
$hierarchical = 1;      
$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 /> ('. $category_id .') <a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a>';

        $args2 = array(
            'taxonomy'     => $taxonomy,
            '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  '<br/> > <a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a>';
                echo apply_filters( 'woocommerce_subcategory_count_html', ' (' . $sub_category->count . ')', $category );


                 $args3 = array(
            'taxonomy'     => $taxonomy,
            'parent'       =>  $sub_category->term_id,
            'orderby'      => $orderby,
            'show_count'   => $show_count,
            'pad_counts'   => $pad_counts,
            'hierarchical' => $hierarchical,
            'title_li'     => $title,
            'hide_empty'   => $empty
        );

        $sub_cats3 = get_categories( $args3 );

        if($sub_cats3) {

            foreach($sub_cats3 as $sub_category3) {
                echo  '<br/> > > <a href="'. get_term_link($sub_category3->slug, 'product_cat') .'">'. $sub_category3->name .'</a>';
                echo apply_filters( 'woocommerce_subcategory_count_html', ' (' . $sub_category3->count . ')', $category );

            }


                 }

            }
        }
    }       
}

答案 2 :(得分:1)

get_categories()使用get_terms(),后者使用WP_Term_Query()

因此,您可以检查WP_Term_Query::__construct()以获得有关已接受参数的信息。

尝试一下:

$args = array(
    'taxonomy' => 'product_cat',
    'get' => 'all'
);
$categories = get_categories($args);

答案 3 :(得分:0)

<?php

  $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 ;
            }   
        }
    }       
}
?>

答案 4 :(得分:0)

要获取所有类别并维护层次结构,您必须使用递归函数。 考虑您有以下 woocommerce 类别层次结构: (类别名称,父级)

服装 0

---配件127

------香水130

---------巴黎 141

---连帽衫 127

---T恤127

装饰 0

音乐 0

未分类 0

编写一个函数来只获取父类别:

function getparentcategories()
    {
        $taxonomy     = 'product_cat';
        $orderby      = 'name';
        $show_count   = 0;      
        $pad_counts   = 0;      
        $hierarchical = 1;      
        $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 );
        return $all_categories;
    }

然后编写第二个递归函数来获取每个父类的子类别,并通过引用传递一个二维数组来保存类别及其级别:

function getcatsubs($items,&$arg,$level=0)
    {
        $taxonomy     = 'product_cat';
        $orderby      = 'name';
        $show_count   = 0;
        $pad_counts   = 0;
        $hierarchical = 1;
        $title        = '';
        $empty        = 0;

        if($items) {
            $category_id = $items->term_id;
            $args = 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($args);
            if($sub_cats)
            {
                foreach ($sub_cats as $sub)
                {
                    $category_id = $sub->term_id;
                    $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
                    );
                    array_push($arg[0],$sub);
                    array_push($arg[1],$level);
                    static::getcatsubs($sub,$arg,$level+1);
                }
            }
            else
            {
                return;
            }
        }
        else
        {
            return;
        }
    }

执行以下代码以获取如上打印的类别层次结构:

$cats=getparentcategories();
foreach ($cats as $cat)
{
    if($cat->parent==0) {
        echo "<p>$cat->name $cat->parent</p>";
        $arg=[[],[]];
        getcatsubs($cat,$arg);
        if ($arg[0]) {
            $l=0;
            foreach ($arg[0] as $scat) {
                $i=$arg[1][$l];
                echo "<p>---";
                for ($x=0;$x<$i;$x++)
                    echo "---";
                echo "$scat->name $scat->parent</p>";
                $l++;
            }
        }
    }
}