【问题标题】:Remove/hide total_sales WooCommerce custom field删除/隐藏 total_sales WooCommerce 自定义字段
【发布时间】:2016-02-24 19:17:13
【问题描述】:

在显示产品的the_meta 时,有没有办法删除total_sales 自定义字段?

我可以将编辑器中的条目更改为另一个名称和值,但它会神奇地再次出现并且不会被删除。

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    我会为此使用“the_meta_key”过滤器。你有几个选项,当然你可以将它们组合起来。

    使用 CSS 控制要隐藏的内容

    add_filter( 'the_meta_key' , 'class_custom_fields', 10, 3);
    
    function classes_custom_fields($string, $key, $value){
        return str_replace('<li>','<li class="' . str_replace(' ', '-', $key). '">',$string);
    }
    
    <style>
    ul.post-meta li.total_sales{
        display:none;
    }
    </style>
    

    通过 PHP 和 CSS 控制要隐藏的内容

    add_filter( 'the_meta_key' , 'hide_custom_fields', 10, 3);
    
    function hide_custom_fields($string, $key, $value){
        $hide_keys = array(
            'total_sales'
        );
        if(in_array(strtolower($key), $hide_keys)){
            return str_replace('<li>','<li class="hide">',$string);
        }
        return $string;
    }
    <style>
        .hide{
            display:none;
        }
    </style>
    

    使用 PHP 控制要显示的内容

    add_filter( 'the_meta_key' , 'allowed_custom_fields', 10, 3);
    
    function allowed_custom_fields($string, $key, $value){
    
        $allowed_keys = array(
            'attribute one',
        );
    
        if(in_array(strtolower($key), $allowed_keys)){
            return $string;
        }
    }
    

    用 PHP 控制不显示的内容

    add_filter( 'the_meta_key' , 'disallowed_custom_fields', 10, 3);
    
    
    function disallowed_custom_fields($string, $key, $value){
    
        $disallowed_keys = array(
            'total_sales'
        );
        if(!in_array(strtolower($key), $disallowed_keys)){
            return $string;
        }
    }
    

    【讨论】:

      【解决方案2】:

      由于total_sales 最终成为ul 组的最后一个列表项(在本例中为ul.post-meta),因此只需输入:ul.post-meta li:last-child{ display: none; }

      【讨论】:

        【解决方案3】:

        更好的选择是将其添加到您的 functions.php 文件中:

        // Hide total sales for Woocommerce products
        delete_post_meta_by_key( 'total_sales' );
        

        【讨论】:

        • 隐藏和删除之间有一个重要的区别。
        猜你喜欢
        • 2018-08-06
        • 1970-01-01
        • 1970-01-01
        • 2015-11-27
        • 2018-10-04
        • 1970-01-01
        • 2021-02-08
        • 2018-10-05
        • 1970-01-01
        相关资源
        最近更新 更多