从 WooCommerce 画廊中删除特色图片
Remove featured image from the WooCommerce gallery
我尝试使用其他帖子的建议,但它们不起作用。我希望不要在我的产品图片 area/image
图库中显示特色产品图片,因为我使用 header 中的特色图片作为背景图片,而且它太宽了,无法显示在图库中。
到目前为止,这是最接近工作的事情,但我收到错误。但是它确实满足了我的需要。
有什么方法可以解决这个问题,这样我就不会收到错误消息了吗?
这是我的代码:
add_filter('woocommerce_single_product_image_thumbnail_html', 'remove_featured_image', 10, 3);
function remove_featured_image($html, $attachment_id, $post_id) {
$featured_image = get_post_thumbnail_id($post_id);
if ($attachment_id != $featured_image) {
return $html;
}
return '';
}
这里是错误:
Missing argument 3 for remove_featured_image() in /home/…/plugins/my-custom-functions/inc/php/functional.php(93) : eval()'d code on line 4
Warning: Missing argument 3 for remove_featured_image() in /home…/plugins/my-custom-functions/inc/php/functional.php(93) : eval()'d code on line 4
woocommerce_single_product_image_thumbnail_html
过滤器挂钩只有 2 个参数。
所以你必须稍微改变你的代码以避免错误,这样:
add_filter('woocommerce_single_product_image_thumbnail_html', 'remove_featured_image', 10, 2);
function remove_featured_image($html, $attachment_id ) {
global $post, $product;
$featured_image = get_post_thumbnail_id( $post->ID );
if ( $attachment_id == $featured_image )
$html = '';
return $html;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
过滤器挂钩参考资料 woocommerce_single_product_image_thumbnail_html
:
- woocommerce 模板:
single-product/product-image.php
- woocommerce 模板:
single-product/product-thumbnails.php
这就是我所做的,希望对您有所帮助。
我使用 Meta Box 插件在产品编辑页面(在 Wordpress 后端)中创建了一个自定义复选框。复选框基本上是“从产品库中隐藏特色图片”,这是代码(将放置在 functions.php 文件中):
function hide_first_img( $meta_boxes ) {
$prefix = 'meta_box';
$meta_boxes[] = array(
'id' => 'hide_img',
'title' => esc_html__( 'Hide first image', 'custom_meta_boxes' ),
'post_types' => array('product'),
'context' => 'advanced',
'priority' => 'default',
'autosave' => 'false',
'fields' => array(
array(
'id' => $prefix . '_hide_first_image',
'name' => esc_html__( 'Hide first image in product gallery', 'custom_meta_boxes' ),
'type' => 'checkbox',
'desc' => esc_html__( 'Check to hide the first image in the gallery', 'custom_meta_boxes' ),
),
),
);
return $meta_boxes;
}
add_filter( 'rwmb_meta_boxes', 'hide_first_img' );
然后我使用了 LoicTheAztec 的代码,效果很好,只在复选框被选中时删除图像,就像这样(放置在 functions.php 文件中):
function remove_featured_image($html, $attachment_id ) {
if( rwmb_meta( 'meta_box_hide_first_image' )){
global $post, $product;
$featured_image = get_post_thumbnail_id( $post->ID );
if ( $attachment_id == $featured_image )
$html = '';
}
return $html;
}
add_filter('woocommerce_single_product_image_thumbnail_html', 'remove_featured_image', 10, 2);
最后,仅针对 FLATSOME THEME 用户,为了也隐藏图库中的第一个缩略图,我直接在我的子主题文件夹中编辑了以下文件:\flatsome-child\woocommerce\single-product\product-gallery-thumbnails.php
也许默认的 Woocomerce 画廊或其他主题有一些类似的文件:
// show first thumbnail only if meta box is NOT checked
<?php if( !rwmb_meta( 'meta_box_hide_first_image' )) : ?>
<div class="col is-nav-selected first">
<a>
<?php
$image_id = get_post_thumbnail_id($post->ID);
$image = wp_get_attachment_image_src( $image_id, apply_filters( 'woocommerce_gallery_thumbnail_size', 'woocommerce_'.$image_size ) );
$image_alt = get_post_meta( $image_id, '_wp_attachment_image_alt', true );
$image = '<img src="'.$image[0].'" alt="'.$image_alt.'" width="'.$gallery_thumbnail['width'].'" height="'.$gallery_thumbnail['height'].'" class="attachment-woocommerce_thumbnail" />';
echo $image;
?>
</a>
</div>
<?php endif; ?>
我可以确认它现在正在我的网站上运行。
我尝试使用其他帖子的建议,但它们不起作用。我希望不要在我的产品图片 area/image
图库中显示特色产品图片,因为我使用 header 中的特色图片作为背景图片,而且它太宽了,无法显示在图库中。
到目前为止,这是最接近工作的事情,但我收到错误。但是它确实满足了我的需要。
有什么方法可以解决这个问题,这样我就不会收到错误消息了吗?
这是我的代码:
add_filter('woocommerce_single_product_image_thumbnail_html', 'remove_featured_image', 10, 3);
function remove_featured_image($html, $attachment_id, $post_id) {
$featured_image = get_post_thumbnail_id($post_id);
if ($attachment_id != $featured_image) {
return $html;
}
return '';
}
这里是错误:
Missing argument 3 for remove_featured_image() in /home/…/plugins/my-custom-functions/inc/php/functional.php(93) : eval()'d code on line 4
Warning: Missing argument 3 for remove_featured_image() in /home…/plugins/my-custom-functions/inc/php/functional.php(93) : eval()'d code on line 4
woocommerce_single_product_image_thumbnail_html
过滤器挂钩只有 2 个参数。
所以你必须稍微改变你的代码以避免错误,这样:
add_filter('woocommerce_single_product_image_thumbnail_html', 'remove_featured_image', 10, 2);
function remove_featured_image($html, $attachment_id ) {
global $post, $product;
$featured_image = get_post_thumbnail_id( $post->ID );
if ( $attachment_id == $featured_image )
$html = '';
return $html;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
过滤器挂钩参考资料 woocommerce_single_product_image_thumbnail_html
:
- woocommerce 模板:
single-product/product-image.php
- woocommerce 模板:
single-product/product-thumbnails.php
这就是我所做的,希望对您有所帮助。 我使用 Meta Box 插件在产品编辑页面(在 Wordpress 后端)中创建了一个自定义复选框。复选框基本上是“从产品库中隐藏特色图片”,这是代码(将放置在 functions.php 文件中):
function hide_first_img( $meta_boxes ) {
$prefix = 'meta_box';
$meta_boxes[] = array(
'id' => 'hide_img',
'title' => esc_html__( 'Hide first image', 'custom_meta_boxes' ),
'post_types' => array('product'),
'context' => 'advanced',
'priority' => 'default',
'autosave' => 'false',
'fields' => array(
array(
'id' => $prefix . '_hide_first_image',
'name' => esc_html__( 'Hide first image in product gallery', 'custom_meta_boxes' ),
'type' => 'checkbox',
'desc' => esc_html__( 'Check to hide the first image in the gallery', 'custom_meta_boxes' ),
),
),
);
return $meta_boxes;
}
add_filter( 'rwmb_meta_boxes', 'hide_first_img' );
然后我使用了 LoicTheAztec 的代码,效果很好,只在复选框被选中时删除图像,就像这样(放置在 functions.php 文件中):
function remove_featured_image($html, $attachment_id ) {
if( rwmb_meta( 'meta_box_hide_first_image' )){
global $post, $product;
$featured_image = get_post_thumbnail_id( $post->ID );
if ( $attachment_id == $featured_image )
$html = '';
}
return $html;
}
add_filter('woocommerce_single_product_image_thumbnail_html', 'remove_featured_image', 10, 2);
最后,仅针对 FLATSOME THEME 用户,为了也隐藏图库中的第一个缩略图,我直接在我的子主题文件夹中编辑了以下文件:\flatsome-child\woocommerce\single-product\product-gallery-thumbnails.php
也许默认的 Woocomerce 画廊或其他主题有一些类似的文件:
// show first thumbnail only if meta box is NOT checked
<?php if( !rwmb_meta( 'meta_box_hide_first_image' )) : ?>
<div class="col is-nav-selected first">
<a>
<?php
$image_id = get_post_thumbnail_id($post->ID);
$image = wp_get_attachment_image_src( $image_id, apply_filters( 'woocommerce_gallery_thumbnail_size', 'woocommerce_'.$image_size ) );
$image_alt = get_post_meta( $image_id, '_wp_attachment_image_alt', true );
$image = '<img src="'.$image[0].'" alt="'.$image_alt.'" width="'.$gallery_thumbnail['width'].'" height="'.$gallery_thumbnail['height'].'" class="attachment-woocommerce_thumbnail" />';
echo $image;
?>
</a>
</div>
<?php endif; ?>
我可以确认它现在正在我的网站上运行。