删除/隐藏total_sales WooCommerce自定义字段
问题描述:
在显示产品的the_meta
时,有没有办法删除total_sales
自定义字段?删除/隐藏total_sales WooCommerce自定义字段
我可以将编辑器中的条目更改为另一个名称和值,但它奇迹般地再次出现并且不会被删除。
答
我会为此使用'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;
}
}
答
由于total_sales
最终成为它的最后一个列表项的ul
组(在这种情况下ul.post-meta
),只需输入:ul.post-meta li:last-child{ display: none; }
答
更好的选择是把它添加到您的functions.php文件:
// Hide total sales for Woocommerce products
delete_post_meta_by_key('total_sales');
隐藏和删除它之间有一个重要的区别。 – 2014-05-16 06:04:31