在顯示產品的the_meta
時,有沒有辦法刪除total_sales
自定義字段?刪除/隱藏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