在我们用wordpress做网站的时候,存储自定义数据,需要用到custom fields,也就是自定义字段。不过,我们在提供给用户使用后台的时候,不能向他们展示custom fields框,一是让用户理解这要怎么用会比较麻烦,二是没有一个友好的界面,使用起来并不方便。
所以,我们在默认情况下,是隐藏掉custom fields框的。
怎么取消掉呢,后台的custom fields框,也是一种meta box,在remove_meta_box函数里有介绍:
remove_meta_box( $id, $page, $context );
$id
(string) (required) Value of the id attribute of the HTML element to remove. Some of the available id values are given below:
‘authordiv’ – Author metabox
‘categorydiv’ – Categories metabox.
‘commentstatusdiv’ – Comments status metabox (discussion)
‘commentsdiv’ – Comments metabox
‘formatdiv’ – Formats metabox
‘pageparentdiv’ – Attributes metabox
‘postcustom’ – Custom fields metabox
‘postexcerpt’ – Excerpt metabox
‘postimagediv’ – Featured image metabox
‘revisionsdiv’ – Revisions metabox
‘slugdiv’ – Slug metabox
‘submitdiv’ – Date, status, and update/save metabox
‘tagsdiv-post_tag’ – Tags metabox
‘{$tax-name}div’ - Hierarchical custom taxonomies metabox
‘trackbacksdiv’ – Trackbacks metabox
…
所以,custom fields用remove_meta_box就可以隐藏,第一个参数换成‘postcustom’即可。
不过,函数使用的到时候,不能简单的在function里面添加这么一句,官方文档有相关实例:
function remove_page_excerpt_field() {
remove_meta_box( 'postexcerpt' , 'page' , 'normal' );
}
add_action( 'admin_menu' , 'remove_page_excerpt_field' );
要在action里使用才可以生效,
而且不要使用register_post_type里面的参数来取消custom fields的显示。