如何使用缩略图拆分子类别

时间:2012-11-24 作者:Dime

这是我用来用缩略图显示所有产品类别的代码:

<?php
$cats = get_terms(\'product_cat\', array(
    \'hide_empty\' => 0,
    \'orderby\' => \'name\'
));
?>
<div class="container"> 
    <?php foreach($cats as $cat) : ?>           
        <?php   global $wp_query;
            if($cat->category_parent == 0) {
                $category_id = $cat->term_id;
                $thumbnail_id   = get_woocommerce_term_meta( $cat->term_id, \'thumbnail_id\', true );
                $image = wp_get_attachment_url( $thumbnail_id );
                echo \'<li><a href="\'. get_term_link($cat->slug, \'product_cat\') .\'">\'. $cat->name .\'</a>\';
                echo \'<img src="\'.$image.\'" width="45px" height="45px" />\';
            }
        ?>      
    <?php endforeach; ?>
</div>
有谁能帮助我如何划分子类别的类别,同时只有缩略图子类别,例如:

类别(文本)

子类别(缩略图)

子类别(缩略图)

1 个回复
最合适的回答,由SO网友:Dime 整理而成

我发现了如何做到这一点:

<?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
);
?>

<?php $all_categories = get_categories( $args );

foreach ($all_categories as $cat) {  

    if($cat->category_parent == 0) {?>


<?php   $category_id = $cat->term_id;
        $thumbnail_id   = get_woocommerce_term_meta( $cat->term_id, \'thumbnail_id\', true );
        $image = wp_get_attachment_url( $thumbnail_id );
        echo \'<h3 style="font-size:16px">\'. $cat->name .\'</h3>\';


        $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) {
        if($sub_cats->$sub_category == 0) {
            $thumbnail_id   = get_woocommerce_term_meta( $sub_category->term_id, \'thumbnail_id\', true );
            $image = wp_get_attachment_url( $thumbnail_id );

            echo \'<li style="list-style-type: none"><a href="\'. get_term_link($sub_category->slug, \'product_cat\') .\'"><img src="\'.$image.\'" width="45px" height="45px" style="margin-right:8px; margin-top:8px; float:left; " /></a></li>\';
            }
        }
        echo \'</li>\';
        }?>

<?php   } 
 } ?>

结束

相关推荐

尝试将分类法添加到Get_Categories(),但不起作用。如何解决这个问题?

我购买了这个模板,我正在尝试自定义。我添加了一个名为location的新分类法类别,并试图用新的分类法类别替换主题中加载的默认类别。以下是当前代码及其加载类别的位置: $cats_array = get_categories(\'hide_empty=0\'); $pages_array = get_pages(\'hide_empty=0\'); $site_pages = array(); $site_cats = array(); foreach (