Have got this working on general wordpress categories using this plugin: https://wordpress.org/plugins/categories-images/ Then adding the following to the category.php template as advised on another thread.

<?php if (function_exists('z_taxonomy_image_url')) { ?>
<img src="<?php echo z_taxonomy_image_url(); ?>" alt="<?php the_title(); ?>" />
<?php } ?>

I'd like to do exactly the same with the product categories. Actually ideally I'd like to be able to add background images to each category so that the description text can go over the top, like the way this shop works: http://www.natures-own.co.uk/Antioxidants/

Is this possible with some minor code tweaking, or better still is there a woocommerce equivalent to the wordpress plugin I've used?

I cannot find any resources for this anywhere, everything I find on searching is referring to just thumbnail of a category list as far as I can see!

Thanks in advance

Pat

有帮助吗?

解决方案

You can add the category image and description by adding the following to the archive-product.php file after <?php if ( apply_filters( 'woocommerce_show_page_title', true ) ) : ?> that if statement:

if (is_product_category())
{
    global $wp_query;
    $cat = $wp_query->get_queried_object();
    $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true ); 
    // get the image URL
    $image = wp_get_attachment_url( $thumbnail_id ); 
    //if you only want the image, uncomment the two lines below
    //list($width, $height) = getimagesize($image);
    //echo '<img src="'.$image.'" alt="" width="'.$width.'" height="'.$height.'"/>';

    $cat_id=$cat->term_id;
    $prod_term=get_term($cat_id,'product_cat');
    $description=$prod_term->description;
    echo '<div class="category-description" style="background-image:url('.$image.');">'.$description.'</div>';
}

其他提示

To display Woocommerce Category image

use this code -

add_action('woocommerce_archive_description', 'woocommerce_add_category_image', 20);
function woocommerce_add_category_image()
{
    global $product;
    if (is_product_category())
    {
        global $wp_query;
        $cat = $wp_query->get_queried_object();
        $thumbnail_id = get_woocommerce_term_meta($cat->term_id, 'thumbnail_id', true);
        $image = wp_get_attachment_url($thumbnail_id);
        if ($image)
        {
            echo '<img src="' . esc_url($image) . '" alt="" />';
        }
    }
   }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top