Piklist 入门教程十六:Media中的Meta Box

先在你的插件parts目录下创建一个media目录,然后新建一个demo-media.php文件,然后跟其他settings时建控件方法一样:代码如下
media

<?php
/*
 Title: My Media MetaBox
 Capability: manage_options
*/

piklist('field', array(
    'type' => 'text'
    ,'scope' => 'post_meta'
    ,'field' => 'field_name'
    ,'label' => 'Example Field'
    ,'description' => 'Field Description'
));

piklist('field', array(
    'type' => 'colorpicker'
    ,'scope' => 'post_meta'
    ,'field' => 'colorpicker_name'
    ,'label' => 'ColorPicker Field'
    ,'description' => 'Color Description'
));

注意,因为media有点特殊,它的metabox 是在你edit时才会显示出来,另外还有现在的piklist 0.7.2版本显示注释属性Title位置错了,论坛已经有人反应,预计下一版本会修正。如下图:
media_box
前台取该metabox值:

$args = array( 'post_type' => 'attachment','post_status' => 'inherit');
$query = new WP_Query($args);
$query2 = get_posts($args);
$query3 = query_posts($args);
var_dump($wp_query->post);
echo get_post_meta($query2[1]->ID, 'field_name', true);

注意:三种取值方式中,因为attachment的特殊性,WP_Query和query_posts都要加上post_status才会查询到正确结果,因为是二次编辑所以post_status参数为inherit,而get_posts可以就一个post_type参数就能查出attachment的内容

文章分类 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