WooCommerce带有图片缩略图的类别列表

时间:2020-05-18 作者:Raptor

我安装了WooCommerce 4,并试图用缩略图列出所有类别。我可以通过插件或小部件来实现它吗?还是我必须自己写代码?

我想要实现的目标与此类似:

enter image description here

提前感谢!

1 个回复
SO网友:Yash Tiwari

Try this code:

    $taxonomyName = "product_cat";

    $parent_terms = get_terms($taxonomyName, array(\'parent\' => 0, \'orderby\' => \'slug\', \'hide_empty\' => false));

    echo \'<ul>\';
    foreach ($parent_terms as $pterm) {


        echo \'<li><a href="\' . get_term_link($pterm->name, $taxonomyName) . \'">\' . $pterm->name . \'</a></li>\';

        $thumbnail_id = get_woocommerce_term_meta($pterm->term_id, \'thumbnail_id\', true);

        $image = wp_get_attachment_url($thumbnail_id);

        echo "<img src=\'{$image}\' alt=\'\' width=\'400\' height=\'400\' />";

        //Get the Child terms
        $terms = get_terms($taxonomyName, array(\'parent\' => $pterm->term_id, \'orderby\' => \'slug\', \'hide_empty\' => false));
        foreach ($terms as $term) {

            echo \'<li><a href="\' . get_term_link($term->name, $taxonomyName) . \'">\' . $term->name . \'</a></li>\';
            $thumbnail_id = get_woocommerce_term_meta($pterm->term_id, \'thumbnail_id\', true);

            $image = wp_get_attachment_url($thumbnail_id);

            echo "<img src=\'{$image}\' alt=\'\' width=\'400\' height=\'400\' />";
        }
    }
    echo \'</ul>\';

相关推荐