WordPress 显示帖子的自定义分类法列
Wordpress display custom taxonomy column for posts
我已经创建了一个自定义 post 类型(书籍),然后是一个自定义分类法 (book_cat),只是针对这个特定的 post 类型。它工作正常,但如果我单击仪表板中的所有书籍页面,则没有列显示书籍被分配给了哪些类别 (book_cat)(如果有)。我需要点击每本书进行编辑并在那里查看。
注册新post类型函数为:
function post_types() {
register_post_type('book', array(
'supports' => array('title', 'editor', 'excerpt'),
'public' => true,
'labels' => array(
'name' => 'Books',
'add_new_item' => 'Add New Book',
'edit_item' => 'Edit Book',
'all_items' => 'All Books',
'singular_name' => 'Book'
),
'menu_icon' => 'dashicons-align-center',
'taxonomies' => array('book_cat')
));
}
add_action('init', 'post_types');
分类法是:
function books_init() {
$labels = array(
'name' => _x( 'Books Cat', 'taxonomy general name' ),
'singular_name' => _x( 'Book Cat', 'taxonomy singular name' ),
'search_items' => __( 'Search Book Cat' ),
'popular_items' => __( 'Popular Book Cat' ),
'all_items' => __( 'All Book Cats' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit BCat' ),
'update_item' => __( 'Update BCat' ),
'add_new_item' => __( 'Add New BCat' ),
'new_item_name' => __( 'New BCat Name' ),
'separate_items_with_commas' => __( 'Separate BCats with commas' ),
'add_or_remove_items' => __( 'Add or remove Bcats' ),
'choose_from_most_used' => __( 'Choose from the most used Bcats' ),
'menu_name' => __( 'Book Cats' ),
);
// create a new taxonomy
register_taxonomy(
'book_cat',
'book',
array(
'labels' => $labels,
'rewrite' => array( 'slug' => 'bcat' ),
)
);
}
add_action( 'init', 'books_init' );
您需要添加 show_admin_column 如下:
// create a new taxonomy
register_taxonomy(
'book_cat',
'book',
array(
'labels' => $labels,
'rewrite' => array( 'slug' => 'bcat' ),
'show_admin_column' => true,
)
);
}
add_action( 'init', 'books_init' );
我已经创建了一个自定义 post 类型(书籍),然后是一个自定义分类法 (book_cat),只是针对这个特定的 post 类型。它工作正常,但如果我单击仪表板中的所有书籍页面,则没有列显示书籍被分配给了哪些类别 (book_cat)(如果有)。我需要点击每本书进行编辑并在那里查看。
注册新post类型函数为:
function post_types() {
register_post_type('book', array(
'supports' => array('title', 'editor', 'excerpt'),
'public' => true,
'labels' => array(
'name' => 'Books',
'add_new_item' => 'Add New Book',
'edit_item' => 'Edit Book',
'all_items' => 'All Books',
'singular_name' => 'Book'
),
'menu_icon' => 'dashicons-align-center',
'taxonomies' => array('book_cat')
));
}
add_action('init', 'post_types');
分类法是:
function books_init() {
$labels = array(
'name' => _x( 'Books Cat', 'taxonomy general name' ),
'singular_name' => _x( 'Book Cat', 'taxonomy singular name' ),
'search_items' => __( 'Search Book Cat' ),
'popular_items' => __( 'Popular Book Cat' ),
'all_items' => __( 'All Book Cats' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit BCat' ),
'update_item' => __( 'Update BCat' ),
'add_new_item' => __( 'Add New BCat' ),
'new_item_name' => __( 'New BCat Name' ),
'separate_items_with_commas' => __( 'Separate BCats with commas' ),
'add_or_remove_items' => __( 'Add or remove Bcats' ),
'choose_from_most_used' => __( 'Choose from the most used Bcats' ),
'menu_name' => __( 'Book Cats' ),
);
// create a new taxonomy
register_taxonomy(
'book_cat',
'book',
array(
'labels' => $labels,
'rewrite' => array( 'slug' => 'bcat' ),
)
);
}
add_action( 'init', 'books_init' );
您需要添加 show_admin_column 如下:
// create a new taxonomy
register_taxonomy(
'book_cat',
'book',
array(
'labels' => $labels,
'rewrite' => array( 'slug' => 'bcat' ),
'show_admin_column' => true,
)
);
}
add_action( 'init', 'books_init' );