Skip to content

Instantly share code, notes, and snippets.

@ShinichiNishikawa
Created November 25, 2012 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShinichiNishikawa/4144842 to your computer and use it in GitHub Desktop.
Save ShinichiNishikawa/4144842 to your computer and use it in GitHub Desktop.
管理画面の投稿一覧に、カスタムタクソノミの列を追加する
// 投稿一覧で表示する項目を操作する
function manage_posts_columns($columns) {
unset($columns['tags']);
unset($columns['comments']);
global $post;
if ( $post->post_type == 'post' ) { // ポストタイプが投稿の時だけ
$date_escape = $columns['date']; // いったん避難
unset($columns['date']); // 消す
$columns['district'] = '地区';
$columns['date'] = $date_escape; // ここで戻すと日付が最後になる
}
return $columns;
}
add_filter( 'manage_posts_columns', 'manage_posts_columns' );
// 新規追加したカスタムタクソノミの列で、タームを取得してリンクを作って表示
function inside_district_column( $column_name ) {
global $post;
if ( $post->post_type == 'post' && $column_name == 'district' ) {
$districts = get_the_terms($post->ID, 'district');
if ( !empty($districts) ) {
$out = array();
foreach ( $districts as $d ) {
$out[] = '<a href="edit.php?district=' . $d->slug . '">' . esc_html(sanitize_term_field('name', $d->name, $d->term_id, 'district', 'display')) . '</a>';
}
echo join( ', ', $out );
} else {
echo '地区無し';
}
}
}
add_action( 'manage_posts_custom_column', 'inside_district_column' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment