gpt4 book ai didi

php - WooCommerce:当元素为空时隐藏 css 属性

转载 作者:行者123 更新时间:2023-11-28 05:21:57 25 4
gpt4 key购买 nike

我试图将自定义字段添加到我网站商店页面的产品列表中。我在 functions.php 文件中使用了这段代码:

add_action( 'woocommerce_after_shop_loop_item_title', 'ins_woocommerce_product_excerpt', 3);  

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}

if (!function_exists('ins_woocommerce_product_excerpt'))
{
function ins_woocommerce_product_excerpt() {

global $post;
$item = get_post_meta( $post->ID, 'item12', true );

if ( $item !== "" || is_home() || is_shop() || is_product_category() || is_product_tag() ) {


echo '<span class="item12"><em>';
echo ( $item );
echo '</em></span>';

}
}
}

代码有效并显示自定义字段(带有 css 制作的绿色标签),在商店页面中的图片产品上。

CSS 代码:

.item12 {
position: absolute;
margin-top: -3.750em;
z-index: 8;
color: #FFFFFF;
display: block;
color: #FFFFFF;
text-shadow: black 0.063em 0.063em 0.313em;
font-weight: 800;
font-size: 1.125em;
background-color: rgba(89, 140, 31, 0.71);
border-radius: 0.438em;
padding: 0.313em 0.313em 0.313em 0.313em;
position: absolute;
/* margin-top: -100px; */
margin-left: 9.375em;
}

问题是当自定义字段为空时,图像上会出现一个绿点。

只有当自定义字段不为空时,如何才能显示标签?

谢谢。

最佳答案

像这样在 function.php 中添加新代码:

add_action( 'woocommerce_after_shop_loop_item_title', 'ins_woocommerce_product_excerpt', 3);  

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}

if (!function_exists('ins_woocommerce_product_excerpt'))
{
function ins_woocommerce_product_excerpt() {

global $post;
$item = get_post_meta( $post->ID, 'item12', true );

if ( $item !== "" || is_home() || is_shop() || is_product_category() || is_product_tag() ) {

if(!empty($item)){
echo '<span class="item12"><em>';
echo ( $item );
echo '</em></span>';
}
}
}
}

关于php - WooCommerce:当元素为空时隐藏 css 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37656809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com