如何为自定义帖子类型定制列表?

时间:2011-09-06 作者:JMichael

例如,如果我有一个名为“Video”的自定义Post类型,并且我想在主(edit.php)表列表中显示该视频的长度,那么如何操作该表中显示的列?

1 个回复
最合适的回答,由SO网友:Milo 整理而成

这类似于向常规帖子添加列,只是过滤器和操作略有不同:manage_edit-{$post_type}_columnsmanage_{$post_type}_posts_custom_column

function wpse27787_add_video_column( $columns ){
    // add a new column to array of columns
    // can also unset columns here to remove them
    $columns[\'length\'] = __(\'Length\');
    return $columns;
}

function wpse27787_manage_video_columns( $column_name, $id ){
    // if this is our custom column, fetch whatever data we want to output
    if( $column_name == \'length\' ):
        // get your video length here using this post\'s $id and
        echo $this_length;
    endif;
}   

function wpse27787_init() {
    // add our filter and action on admin_init
    add_filter( \'manage_edit-video_columns\', \'wpse27787_add_video_column\' );
    add_action( \'manage_video_posts_custom_column\', \'wpse27787_manage_video_columns\', 10, 2 );
}
add_action( \'admin_init\' , \'wpse27787_init\' );

EDIT -

如果要对列重新排序,请使用原始列的值创建一个新数组,将列添加到所需位置:

function wpse27787_add_video_column( $columns ){

    foreach( $columns as $key => $val):
        $reordered_columns[$key] = $val;
        if( $key == \'title\' ):
            // add our custom column after title
            $reordered_columns[\'length\'] = __(\'Length\');
        endif;
    endforeach;

    return $reordered_columns;
}

结束

相关推荐