Piklist入门教程九:不同的Post Status 显示Meta Boxes和 Fields

在功能强大的Web应用中,你有时候只想在post的不同status上显示不同的Meta Box 或Meta Field。这个功能可以用Piklist的capability参数,详情查看《Piklist中文手册》的metabox文件结构,你可以通过用user role 或 capability来显示/隐藏 fields。例如:一个订单系统,在流程中,订单不同状态显示不同的fields,Piklist很容易做到这一点。
在本教程,你将学习到在不同的Post status中显示隐藏Meta box 或 Field。
1.在meta-boxes目录下,新建一个”status-metabox.php”

status_file

2.Piklist中可以用两种方法来实现,一种是在注释属性中的status参数,是用来显示/隐藏Meta box 另一种是在Fields中用on_post_status参数中的’hide’实现显示/隐藏
Post Status 显示/隐藏 整个Meta Box

<?php
/*
Title: My Status Meta Box
Post Type: product
Status: draft
*/

status_metabox
注意:虽然WordPress默认状态是draft,但是你New Product时,Meta Box也不会显示的,你要先保存一下,再编辑打开就能看到上图显示出来的Meta Box。不同的status中用逗号分格,中间不要有空格,
如下:

<?php
/*
Title: My Status Meta Box
Post Type: product
Status:complete,repair
*/

Post Status 显示/隐藏 Meta Field
你可以在不同的status来显示/隐藏个别fields,给控件加上on_post_status参数,下面代码中添加三个text控件,然后在estimate,complete,repair状态间显示各自的text控件,下面代码里分别演示了单个status和多个status时的状态,注意多个不连续时用个数组形式,逗号分格。而连续status的话就中间–两个中划线。最后多动手切换不同状态看看效果吧。
status-metabox.php全部代码

<?php
/*
Title: My Status Meta Box
Post Type: product
*/

piklist('field', array(
    'type' => 'text'
    ,'scope' => 'post_meta'
    ,'field' => 'product1_text'
    ,'label' => 'Text box'
    ,'description' => 'Product 1'
    ,'value' => 'Product 1'
    ,'on_post_status' => array(
            'hide' => 'complete'   //单个status
    )
));

piklist('field', array(
    'type' => 'text'
    ,'scope' => 'post_meta'
    ,'field' => 'product2_text'
    ,'label' => 'Text box'
    ,'description' => 'Product 2'
    ,'value' => 'Product 2'
    ,'on_post_status' => array(
        'hide' => array('estimate','repair') //多个不连续status
    )
));

piklist('field', array(
    'type' => 'text'
    ,'scope' => 'post_meta'
    ,'field' => 'repair_text'
    ,'label' => 'Repair Text box'
    ,'description' => 'Product 3'
    ,'value' => 'Product 3'
    ,'on_post_status' => array(
        'hide' => 'estimate--repair' //多个连续status
    )
));

效果图:

status_1

status_2

文章分类 Piklist, 经验分享 标签: ,

发表评论


Warning: Use of undefined constant XML - assumed 'XML' (this will throw an Error in a future version of PHP) in /var/www/wp/code/wp-content/plugins/wp-syntaxhighlighter/wp-syntaxhighlighter.php on line 1048