2011-11-11 12 views
8

というカスタム投稿タイプを作成しました。これにより、ロケーションという新しいタクソノミーが登録され、バナーが表示されるページを指定します。すべてが素晴らしいですが、管理画面でカスタム投稿タイプ 'バナー'をクリックすると、作成されたすべてのバナーが表示されますが、テーブルにはタクソノミー 'ロケーション'の列がありません。カスタム投稿タイプのリストにカスタムタクソノミー列を表示

つまり、バナーの一覧に、バナーの場所が表示されます。

答えて

7

manage_post-type_custom_columnおよびmanage_edit_post-type_columnsフィルタを使用すると、分類タイプの列を投稿タイプのリストに追加できます。興味のある方、register_taxonomy機能については

add_action('admin_init', 'my_admin_init'); 
function my_admin_init() { 
    add_filter('manage_edit-banner_columns', 'my_new_custom_post_column'); 
    add_action('manage_banner_custom_column', 'location_tax_column_info', 10, 2); 
} 

function my_new_custom_post_column($column) { 
    $column['location'] = 'Location'; 

    return $column; 
} 

function location_tax_column_info($column_name, $post_id) { 
     $taxonomy = $column_name; 
     $post_type = get_post_type($post_id); 
     $terms = get_the_terms($post_id, $taxonomy); 

     if (!empty($terms)) { 
      foreach ($terms as $term) 
      $post_terms[] ="<a href='edit.php?post_type={$post_type}&{$taxonomy}={$term->slug}'> " .esc_html(sanitize_term_field('name', $term->name, $term->term_id, $taxonomy, 'edit')) . "</a>"; 
      echo join('', $post_terms); 
     } 
     else echo '<i>No Location Set. </i>'; 
} 
+0

'manage_post-type_custom_column'フィルタは、' hierarchical => true'(Pagesなど)のカスタム投稿タイプに対してのみ機能することに注意してください。 'hierarchical => false'(ポストのような)のCPTでは、非常に似たフィルタ' manage_post-type_posts_custom_column'を使うべきです。 ので、この特定の例では、あなたが読むためにライン4を変更したい: 'add_action( 'manage_banner_posts_custom_column'、 'location_tax_column_info'、10、2); [ここ]' 詳細情報(のhttp://コーデックス。 wordpress.org/Plugin_API/Action_Reference/manage_posts_custom_column) – Tim

17

WordPress 3.5のように、今show_admin_column(デフォルトはfalse)の引数を提供しています。 trueに設定すると、管理者にタクソノミ列が自動的に表示されます。

1
//what version of wordpress you are using 
//since wp 3.5 you can pass parameter show_admin_column=>true 
// hook into the init action and call create_book_taxonomies when it fires 
add_action('init', 'create_book_taxonomies', 0); 

// create two taxonomies, genres and writers for the post type "book" 
function create_book_taxonomies() { 
// Add new taxonomy, make it hierarchical (like categories) 
$labels = array(
    'name'    => _x('Genres', 'taxonomy general name'), 
    'singular_name'  => _x('Genre', 'taxonomy singular name'), 
    'search_items'  => __('Search Genres'), 
    'all_items'   => __('All Genres'), 
    'parent_item'  => __('Parent Genre'), 
    'parent_item_colon' => __('Parent Genre:'), 
    'edit_item'   => __('Edit Genre'), 
    'update_item'  => __('Update Genre'), 
    'add_new_item'  => __('Add New Genre'), 
    'new_item_name'  => __('New Genre Name'), 
    'menu_name'   => __('Genre'), 
); 

$args = array(
    'hierarchical'  => true, 
    'labels'   => $labels, 
    'show_ui'   => true, 
    'show_admin_column' => true, 
    'query_var'   => true, 
    'rewrite'   => array('slug' => 'genre'), 
); 

register_taxonomy('genre', array('book'), $args); 
}