首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >删除Woocommerce中的产品类别占位符图像

删除Woocommerce中的产品类别占位符图像
EN

Stack Overflow用户
提问于 2018-12-14 05:10:18
回答 1查看 2.2K关注 0票数 1

我正在尝试摆脱商店页面上的占位符产品类别图像。

现在,我正在使用这段代码向没有图像的产品添加一个.no-image类,这样我就可以对它们进行不同的样式设置。这很有效,我想对类别做同样的事情。

代码语言:javascript
复制
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 );

我试着编辑代码来检测类别,但是我不能让它工作。我做错了什么?

代码语言:javascript
复制
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 );
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-14 10:11:21

要使其适用于移除占位符图像和添加自定义<div>容器的产品类别,需要使用不同的方法,因为产品类别使用特定于content_product_cat.php的模板:

代码语言:javascript
复制
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'] ) . '" />';
        }
    }
}

代码放在活动子主题(或活动主题)的function.php文件中。经过测试,效果良好。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53770113

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档