简体   繁体   English

如何在自定义帖子类型的类别网格中添加自定义列?

[英]How to add custom column in category grid of custom post type?

I'm trying to add the category ID to the category list table for a custom post type using manage_{$taxonomy}_custom_column , and I found out that the function is now deprecated. 我正在尝试使用manage_{$taxonomy}_custom_column将类别ID添加到自定义帖子类型的类别列表中,并且发现该功能现在已被弃用。

The filter is now red on the following page with no usage information available: https://codex.wordpress.org/Plugin_API/Action_Reference/manage_posts_custom_column 现在,下一页上的过滤器为红色,没有可用的使用信息: https : //codex.wordpress.org/Plugin_API/Action_Reference/manage_posts_custom_column

And the following page says the filter is now deprecated, with no followup information since version 3: http://adambrown.info/p/wp_hooks/hook/manage_ {$taxonomy}_custom_column 下一页说该过滤器现在已被弃用,自版本3起没有任何后续信息: http ://adambrown.info/p/wp_hooks/hook/manage_ {$ taxonomy} _custom_column

I came across this solution on github, but it doesn't work: https://gist.github.com/maxfenton/593473788c2259209694 我在github上遇到了这个解决方案,但是它不起作用: https : //gist.github.com/maxfenton/593473788c2259209694

I've not found any solution that serves as a replacement for manage_{$taxonomy}_custom_column . 我找不到任何可替代manage_{$taxonomy}_custom_column解决方案。 Does anyone have any idea how to go about adding a category ID column in the category grid view? 有谁知道如何在类别网格视图中添加类别ID列?

Here is a screenshot showing where I want to add the ID: 这是显示我要在其中添加ID的屏幕截图:

在此处输入图片说明

Edited the code: 编辑代码:

add_filter('manage_edit-{custom_post_type}_columns', 'mytheme_categoy_id_column', 10, 2);

if (!function_exists('mytheme_categoy_id_column')) {
    function mytheme_categoy_id_column($cat_columns){
        $cat_columns['cat_id'] = esc_html__('Category ID', 'mytheme');
        return $cat_columns;
    }
}

add_filter ('manage_{custom_post_type}_custom_column', 'mytheme_custom_column_content', 10,3);

if (!function_exists('mytheme_custom_column_content')) {
    function mytheme_custom_column_content($deprecated, $column_name, $term_id){
        if ($column_name == 'cat_id') {
            return $term_id;
        }
    }
}

Try this 尝试这个

// To show the column header
function custom_column_header( $columns ){
  $columns['header_name'] = 'Header Name for Display'; 
  return $columns;
}

add_filter( "manage_edit-(your-texanomy)_columns", 'custom_column_header', 10);

// To show the column value
function custom_column_content( $value, $column_name, $tax_id ){
   return $tax_id ;
}
add_action( "manage_(your-texanomy)_custom_column", 'custom_column_content', 10, 3);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM