dsaff82024 2016-06-06 11:55
浏览 48
已采纳

WooCommerce:当元素为空时隐藏css的适当性

I tried to add a custom field into the products list in the shop page of my website. I used this code in functions.php file:

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>';

        }
    }
}   

The code works and shows the custom field (with a green label made in css), over the image product in the shop page.

css codes:

.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;
}

The problem is when the custom field is empty, a green dot is visibile over the image.

How can I do to show the label only when the custom field is not empty?

Thanks.

  • 写回答

1条回答 默认 最新

  • douzhaishan5462 2016-06-06 12:07
    关注

    Add New Code in function.php Like This:

    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>';
                }
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?