J'essaie de me débarrasser des images de catégorie de produit d'espace réservé sur la page de la boutique.
En ce moment, j'utilise ce code pour ajouter une classe .no-image aux produits qui n'ont pas d'image afin que je puisse les styliser différemment. Cela fonctionne très bien, je veux faire la même chose pour les catégories.
function before_imageless_product() {
if( !has_post_thumbnail( get_the_id() ) ){
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_category_thumbnail', 10 );
echo '<div class="no-product-image">';
}
}
add_action( 'woocommerce_before_shop_loop_item', 'before_imageless_product', 9 );
function after_imageless_product() {
if( !has_post_thumbnail( get_the_id() ) ){
add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_category_thumbnail', 10 );
echo '</div>';
}
}
add_action( 'woocommerce_after_shop_loop_item', 'after_imageless_product', 9 );
J'ai essayé de modifier le code pour détecter les catégories mais je ne peux pas le faire fonctionner. Qu'est-ce que je fais mal?
function before_imageless_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( !$thumbnail_id ){
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_category_thumbnail', 10 );
echo '<div class="no-category-image">';
}
}
add_action( 'woocommerce_before_shop_loop_item', 'before_imageless_category', 9 );
function after_imageless_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( !$thumbnail_id ){
add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_category_thumbnail', 10 );
echo '</div>';
}
}
add_action( 'woocommerce_after_shop_loop_item', 'after_imageless_category', 9 );
Pour que cela fonctionne pour les catégories de produits en supprimant l'image d'espace réservé et en ajoutant un <div>
conteneur personnalisé , cela doit être fait différemment car les catégories de produits utilisent content_product_cat.php
un modèle spécifique:
add_action( 'woocommerce_before_subcategory', 'before_imageless_category', 9, 1 );
function before_imageless_category( $category ) {
if( ! get_woocommerce_term_meta( $category->term_id, 'thumbnail_id', true ) ) {
echo '<div class="no-category-image">';
}
remove_action('woocommerce_before_subcategory_title', 'woocommerce_subcategory_thumbnail', 10 );
add_action('woocommerce_before_subcategory_title', 'custom_subcategory_thumbnail', 10, 1 );
}
add_action( 'woocommerce_after_subcategory', 'after_imageless_category', 11, 1 );
function after_imageless_category( $category ) {
if( ! get_woocommerce_term_meta( $category->term_id, 'thumbnail_id', true ) ) {
echo '</div>';
}
}
function custom_subcategory_thumbnail( $category ) {
$small_thumbnail_size = apply_filters( 'subcategory_archive_thumbnail_size', 'woocommerce_thumbnail' );
$dimensions = wc_get_image_size( $small_thumbnail_size );
$thumbnail_id = get_woocommerce_term_meta( $category->term_id, 'thumbnail_id', true );
if ( $thumbnail_id ) {
$image = wp_get_attachment_image_src( $thumbnail_id, $small_thumbnail_size );
$image = $image[0];
$image_srcset = function_exists( 'wp_get_attachment_image_srcset' ) ? wp_get_attachment_image_srcset( $thumbnail_id, $small_thumbnail_size ) : false;
$image_sizes = function_exists( 'wp_get_attachment_image_sizes' ) ? wp_get_attachment_image_sizes( $thumbnail_id, $small_thumbnail_size ) : false;
} else {
return;
}
if ( $image ) {
// Prevent esc_url from breaking spaces in urls for image embeds.
// Ref: https://core.trac.wordpress.org/ticket/23605.
$image = str_replace( ' ', '%20', $image );
// Add responsive image markup if available.
if ( $image_srcset && $image_sizes ) {
echo '<img src="' . esc_url( $image ) . '" alt="' . esc_attr( $category->name ) . '" width="' . esc_attr( $dimensions['width'] ) . '" height="' . esc_attr( $dimensions['height'] ) . '" srcset="' . esc_attr( $image_srcset ) . '" sizes="' . esc_attr( $image_sizes ) . '" />';
} else {
echo '<img src="' . esc_url( $image ) . '" alt="' . esc_attr( $category->name ) . '" width="' . esc_attr( $dimensions['width'] ) . '" height="' . esc_attr( $dimensions['height'] ) . '" />';
}
}
}
Le code va dans le fichier function.php de votre thème enfant actif (ou thème actif). Testé et fonctionne.
Cet article est collecté sur Internet, veuillez indiquer la source lors de la réimpression.
En cas d'infraction, veuillez [email protected] Supprimer.
laisse moi dire quelques mots