2011-11-11 73 views
8

Tôi đã tạo loại bài đăng tùy chỉnh có tên là biểu ngữ. Qua đó tôi đăng ký phân loại mới có tên là vị trí chỉ định trang nào sẽ được hiển thị trên trang nào. Tất cả mọi thứ là tuyệt vời tuy nhiên khi tôi bấm vào tùy chỉnh bài viết loại 'Banner' trong cửa sổ quản trị tôi thấy tất cả các biểu ngữ được tạo ra tuy nhiên bảng không có một cột cho phân loại 'Location'.Hiển thị cột phân loại tùy chỉnh trong danh sách loại bài đăng tùy chỉnh

Nói cách khác, tôi muốn có thể xem vị trí của biểu ngữ trong danh sách biểu ngữ.

Trả lời

7

Bạn có thể sử dụng bộ lọc manage_post-type_custom_column và manage_edit_post-type_columns để thêm cột phân loại vào danh sách loại bài đăng.

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

Chỉ cần lưu ý rằng bộ lọc 'manage_post-type_custom_column' chỉ hoạt động cho Loại bài đăng tùy chỉnh nơi' hierarchical => true' (như Pages). Đối với CPT, trong đó 'hierarchical => false' (như Posts), bạn nên sử dụng bộ lọc rất giống' manage_post-type_posts_custom_column'. Vì vậy, trong ví dụ cụ thể này, bạn nên thay đổi dòng 4 thành: 'add_action ('manage_banner_posts_custom_column', 'location_tax_column_info', 10, 2);' Thông tin thêm [tại đây] (http: // codex. wordpress.org/Plugin_API/Action_Reference/manage_posts_custom_column) – Tim

17

Đối với những người quan tâm, các register_taxonomy chức năng, tính đến WordPress 3.5, hiện nay cung cấp một cuộc tranh cãi cho show_admin_column (false theo mặc định). Đặt thành true, nó sẽ tự động hiển thị cột phân loại trong quản trị viên.

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); 
} 
Các vấn đề liên quan