wordpress 循环所有产品类别名称和类别缩略图(woocommerce),就像post循环一样

gcxthw6b  于 2022-11-02  发布在  WordPress
关注(0)|答案(2)|浏览(210)

我想制定一个类别划分如附图。
循环所有产品类别名称和类别缩略图(woocommerce),就像post循环一样。
任何人都可以帮助我??

`

<?php
                            $args = array(
                                // 'posts_per_page' => -1,
                                'taxonomy' => 'product_cat',
                                'orderby' => 'name',
                                // 'show_count' => 0,
                                // 'pad_counts' => 0,
                                // 'hierarchical' => 1,
                                // 'title_li' => '',
                                // 'hide_empty' => 0,
                            );
                        $loop = new WP_Query( $args );
                        while ( $loop->has_category( $category = '', $post = null ) ) : $loop->the_post(); 
                        global $product; ?> 
                        <!-- !.p-item -->
                        <li class="p-item">
                            <a href="<?php get_category_link($loop->post->ID); ?>">
                                <?php if ( has_post_thumbnail( $loop->post->ID ) ) {
                                    echo get_the_post_thumbnail($loop->post->ID, 'ihossain');
                                    } else { ?>
                                    <img src="<?php bloginfo('template_directory'); ?>/img/icon_category_image_1.svg" alt="<?php the_title(); ?>" />
                                <?php } ?>
                                <h6 class="icon-title"><?php $categoo = $product->get_categories(); echo $categoo; ?></h6>
                            </a>
                        </li>
                        <!-- !!.p-item -->
                        <?php endwhile; ?>

`

ivqmmu1c

ivqmmu1c1#

您可以使用此代码访问所有类别及其缩略图。
“产品类别”;

$prod_categories = get_terms($taxonomyName, array(

'orderby'=> 'name',

'order' => 'DESC',

'hide_empty' => 1

));
foreach($prod_categories作为$prod_cat){

if ( $prod_cat->parent != 0 ){

如果是,则返回一个新的标签。

$cat_thumb_url = wp_get_attachment_image_src( $cat_thumb, 'full' );

$term_link = get_term_link( $prod_cat, 'product_cat' );

}
}

dxxyhpgq

dxxyhpgq2#

试试看:

add_shortcode('category_list', 'get_category_list');

function get_category_list()
{
    $taxonomyName = "product_cat";
    //This gets top layer terms only.  This is done by setting parent to 0.
    $parent_terms = get_terms($taxonomyName, array('parent' => 0, 'orderby' => 'slug', 'hide_empty' => 0));

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

        //show parent categories
        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);
        // get the image URL for parent category
        $image = wp_get_attachment_url($thumbnail_id);
        // print the IMG HTML for parent category
        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);
            // get the image URL for child category
            $image = wp_get_attachment_url($thumbnail_id);
            // print the IMG HTML for child category
            echo "<img src='{$image}' alt='' width='400' height='400' />";
        }
    }
    echo '</ul>';
}

相关问题