【发布时间】:2021-12-30 00:57:23
【问题描述】:
我在 Functions.php 中的代码显示存储在相应 WooCommerce 产品上的自定义字段(使用高级自定义字段插件)中的文本。
我的问题是,我只能在商店/产品档案中显示这个。当我尝试在单个产品页面缩略图中挂钩时,它不起作用。
如何挂钩到主单产品页面图像?
我在functions.php中的代码
function wpo_before_shop_loop_item_title() {
if (defined('WPO_IS_ENABLED') && (WPO_IS_ENABLED === false)) {
// Disabled by configuration.
} else {
$post_id = get_the_ID();
// Is this a featured product?
$terms = wp_get_post_terms(
$post_id,
'product_visibility',
array('fields' => 'names')
);
$is_featured = in_array('featured', $terms);
// Get our custom overlay properties.
$custom_overlay_text = get_post_meta($post_id, 'product_overlay_text', true);
$is_custom_overlay_visible = boolval(get_post_meta(
$post_id,
'is_product_overlay_enabled',
true
));
// Featured product star.
if ($is_featured) {
echo '<div class="prod-overlay prod-overlay-feat">';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '</div>'; // .prod-overlay
}
// Custom overlay text.
if ($is_custom_overlay_visible && !empty($custom_overlay_text)) {
echo '<div class="prod-overlay prod-overlay-text">';
printf(
'<span class="prod-overlay-label">%s</span>',
esc_html($custom_overlay_text)
);
echo '</div>'; // .prod-overlay
}
}
}
add_action(
'woocommerce_before_shop_loop_item_title',
'wpo_before_shop_loop_item_title', 'woocommerce_before_add_to_cart_button', 'woocommerce_product_thumbnails' ,
9
);
【问题讨论】:
-
请同时添加您为覆盖文本添加的 CSS。
标签: php wordpress woocommerce