如何在列表中显示元框字段

时间:2013-12-03 13:41:15

标签: php wordpress

我正在使用register_post_type创建一个post_type,所以我得到add_new选项,我使用meta_box来使用我的自定义字段。但在我的列表中,它只显示标题列,我如何使用我的meta_box字段作为列表中的列页。 我的register_post_type代码是 -

// Create Brand Management
add_action('init', 'manage_brand');
function manage_brand() {
    register_post_type('brand', array(
        'labels' => array(
            'name' => 'Manage Brand',
            'singular_name' => 'Manage Brand',
            'add_new' => 'Add New',
            'add_new_item' => 'Add New Brand',
            'edit' => 'Edit',
            'edit_item' => 'Edit Brand',
            'new_item' => 'New Brand',
            'view' => 'View',
            'view_item' => 'View Brand',
            'search_items' => 'Search Brand',
            'not_found' => 'No Brand',
            'not_found_in_trash' => 'No Brand found in Trash',
            'parent' => 'Parent News Brand'
        ),
        'public' => true,
        'menu_position' => 100,
        'supports' => array('','thumbnail'),
        'taxonomies' => array('project-cat'),
        'menu_icon' => plugins_url('images/adv-.png', __FILE__),
        'has_archive' => true,


            )
    );
}

我使用meta_box添加字段的代码是 -

//add meta data for brand
add_action('admin_init', 'brand_register_meta_boxes');

function brand_register_meta_boxes() {
if (!class_exists('RW_Meta_Box'))
        return;
    $prefix = 'brand_';

    $meta_boxes[] = array(
       'title' => 'Add Brand',
        'pages' => array('brand'),

        'fields' => array(

            array(
            'name' => __( 'Brand Name', 'rwmb' ),
            'desc' => __( 'Add Brand Name', 'rwmb' ),
            'id'   => "{$prefix}code",
            'type' => 'text',
            'required' => true,

            ), 

        )
    );     
        foreach ($meta_boxes as $meta_box) {
        new RW_Meta_Box($meta_box);
    }

}

我的列表页面是这样的,只有瓷砖 - Image of my admin post_type listing page

如何在此商家信息中添加我的meta_box字段。

1 个答案:

答案 0 :(得分:1)

假设元框的值存储为post meta,您应该(a)使用动态manage_{$post_type}_posts_columns过滤器钩子注册自定义列:

add_filter( 'manage_brand_posts_columns', 'so20352744_manage_brand_posts_columns', 25, 1 );
function so20352744_manage_brand_posts_columns( $cols )
{
    $cols['brand'] = __( 'Brand', 'txt_domain' );

    return $cols;
}

和(b)使用manage_posts_custom_column操作挂钩向列中添加内容:

add_action( 'manage_posts_custom_column', 'so20352744_manage_posts_custom_column', 2, 1 );
function so20352744_manage_posts_custom_column( $col )
{
    global $post;

    switch ( $col )
    {
        case "brand" :

            if( '' != get_post_meta( $post->ID, 'brand_code', true ) )
                echo get_post_meta( $post->ID, 'brand_code', true );
            else
                echo __( 'N/A', 'txt_domain' );

            break;

        default :
            break;
    }
}

这两个函数都在functions.php。更多信息:http://codex.wordpress.org/Plugin_API/Action_Reference/manage_posts_custom_column