【发布时间】:2019-05-15 03:46:24
【问题描述】:
我正在尝试删除商店页面上的占位符产品类别图片。
现在我正在使用此代码为没有图像的产品添加一个 .no-image 类,以便我可以对它们进行不同的样式设置。这很好用,我想对类别做同样的事情。
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 );
我尝试编辑代码以检测类别,但无法正常工作。 我做错了什么?
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 );
【问题讨论】:
标签: wordpress loops woocommerce thumbnails categories