wordpress - 将自定义分类法添加到所有帖子页面
问题描述
我有一个自定义帖子类型,员工,并且这个帖子类型有一个自定义分类,角色。我希望能够在 wp-admin 后端的所有员工页面上按/查看角色进行排序,例如默认情况下类别和标签如何为帖子工作。
谢谢
自定义帖子类型
function register_staff(){
$labels = array(
'name' => 'Staff',
'singular_name' => 'Staff',
'add_new' => 'Add Staff',
'all_items' => 'All Staff',
'add_new_item' => 'Add Staff Member',
'edit_item' => 'Edit Staff Member',
'new_item' => 'New Staff Member',
'view_item' => 'View staff',
'search_item' => 'Search staff',
'not_found' => 'No Items Found',
'not_found_in_trash' => 'No staff found in trash',
);
$args = array(
'labels' => $labels,
'public' => false,
'show_ui' => true,
'has_index' => true,
'has_archive' => true,
'publicly_queryable' => true,
'query_var' => true,
'has_archive' => true,
'rewrite' => true,
'capability_type' => "post",
'hierarchical' => false,
'supports' => array(
'title',
'editor',
'page-attributes',
'excerpt',
'thumbnail',
'revisions',
),
'taxonomies' => array("role"),
'menu_position' => 5,
'menu_icon' => "dashicons-businessperson",
'exclude_from_search' => false,
);
register_post_type('staff', $args);
}
add_action( "init", "register_staff");
自定义分类
add_action( "init", "register_staff");
function build_taxonomies() {
register_taxonomy('role', 'staff', array(
'label' => 'Roles',
'public' => true,
));
}
add_action( 'init', 'build_taxonomies', 0 );
解决方案
请参阅https://codex.wordpress.org/Plugin_API/Action_Reference/manage_$post_type_posts_custom_column
像这样未经测试:
if ( !function_exists('AddTaxColumn') ) {
function AddTaxColumn($cols) {
$cols['yourtaxonomy'] = __('My Taxonomy');
return $cols;
}
function AddTaxValue($column_name, $post_id) {
if ( 'yourtaxonomy' == $column_name ) {
$tax_id = wp_get_post_terms( $post_id, 'yourtaxonomy' );
if ($tax_id) {
$taxonomies = join( ", ", $tax_id );
echo $taxonomies;
}
}
}
add_filter( 'manage_team_posts_columns', 'AddTaxColumn' );
add_action( 'manage_team_posts_custom_column', 'AddTaxValue', 10, 2 );
add_filter( 'manage_edit-team_sortable_columns', 'AddTaxColumn' );
}
推荐阅读
- python - 在 Windows 10 的 Python 3.8.2 文件夹中找不到“脚本”文件夹或“pip”文件
- kivy - Kivy:Widget.on_touch_down 按钮
- python - Pandas:grouby 和 sort(升序和降序混合)
- hazelcast - 有可能将“cacheResolver”与 hazelcast 一起使用
- git - git reset HEAD 文件没有停止目标文件
- docker - 在 S3 中备份在 Docker 上运行的 ElasticSearch 的最佳方法
- android - WrappingUtils: 不知道怎么圆那个drawable:com.facebook.drawee.drawable.RoundedColorDrawable?
- node.js - 使用 react 和 node 进行 Firebase 身份验证
- python - 如何在 Matplotlib Finance 中的图表上绘图?
- nginx - Nuxtjs 和 Nginx 代理下载没有“http://”的文件