wordpress管理画面の、投稿一覧に新しくカスタムフィールドを表示する列を追加してくれと言われたら。
/**
* 新しい列を日付の前に追加。
*/
function add_cfield_on_posts_columns($columns) {
global $post;
if ( $post->post_type == 'post' ) {
$date_escape = $columns['date'];
unset($columns['date']);
$columns['new_custom_field'] = "Custom field";
$columns['date'] = $date_escape;
}
return $columns;
}
/**
* 作った列にカスタムフィールドの値を表示
*/
function show_cfield_on_the_new_column( $column_name ) {
global $post;
$columns = array(
array(
'col' => 'new_custom_field',
'cf' => 'custom_field_name'
),
);
foreach ($columns as $key => $value) {
if ( $post->post_type == 'post' && $column_name == $value['col'] ) {
$custom_field = $value['cf'];
$new_custom_field = get_post_meta($post->ID, $custom_field, true);
echo $new_custom_field;
}
}
}
add_filter( 'manage_posts_columns', 'add_cfield_on_posts_columns' );
add_action( 'manage_posts_custom_column', 'add_new_column' );