2011-11-11 168 views
8

我創建了一個名爲橫幅的自定義帖子類型。因此我註冊了一個名爲的地點的新分類標準,該分類標準指定了要在哪個頁面上顯示橫幅。一切都很好,但是當我點擊管理窗口中的自定義帖子類型'橫幅'時,我看到所有創建的橫幅,但是該表沒有分類'位置'的列。在自定義帖子類型列表中顯示自定義分類列

換句話說,我希望能夠看到橫幅列表中橫幅的位置。

回答

7

您可以使用manage_post-type_custom_column和manage_edit_post-type_columns過濾器將分類標準列添加到帖子類型列表中。

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(如Posts)的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

對於那些有興趣的register_taxonomy功能,爲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); 
} 
相關問題