How can I set the Image of woocommerce category as

2019-09-08 19:29发布

问题:

This question already has an answer here:

  • How to display Woocommerce Category image? 8 answers

I want to set the woocommerce category image as the title background.

This is the title line in archive-product.php

<h1 class="page-title"><?php woocommerce_page_title(); ?></h1>

I wrote

                <h1 style="background: url(<?php 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 ); 

    echo "<img src='{$image}' alt='' width='762' height='365' />";
}   
?>);" class="page-title"><?php woocommerce_page_title(); ?></h1>

I can see the image in the browser editor! But something is wrong. How could I resolve that?

回答1:

<?php if ( apply_filters( 'woocommerce_show_page_title', true ) ) : ?>

            <h1 style="background: url(<?php 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 ); 

    echo "'{$image}'";
}   
?>);" class="page-title"><?php woocommerce_page_title(); ?></h1>

I followed How to display Woocommerce Category image? and set echo "'{$image}'";
instead of echo "<img src='{$image}' alt='' width='762' height='365' />";