【问题标题】:How to add custom column in category grid of custom post type?如何在自定义帖子类型的类别网格中添加自定义列?
【发布时间】:2016-07-19 18:36:05
【问题描述】:

我正在尝试使用 manage_{$taxonomy}_custom_column 将类别 ID 添加到自定义帖子类型的类别列表中,但我发现该功能现已弃用。

过滤器现在在以下页面上显示为红色,没有可用的使用信息: https://codex.wordpress.org/Plugin_API/Action_Reference/manage_posts_custom_column

以下页面显示过滤器现已弃用,自版本 3 以来没有后续信息: http://adambrown.info/p/wp_hooks/hook/manage_{$taxonomy}_custom_column

我在 github 上遇到了这个解决方案,但它不起作用:https://gist.github.com/maxfenton/593473788c2259209694

我没有找到任何可以替代manage_{$taxonomy}_custom_column 的解决方案。有谁知道如何在类别网格视图中添加类别 ID 列?

这是我要添加 ID 的屏幕截图:

【问题讨论】:

  • 它在哪里说它已被弃用?

标签: php wordpress custom-post-type


【解决方案1】:

修改了代码:

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;
        }
    }
}

【讨论】:

  • 感谢您发布解决方案 dingo,但我需要有关分类网格的帮助,而不是专门的自定义帖子类型网格。 snag.gy/JGF7g.jpg
  • 谢谢你的野狗。我投了赞成票,但 Shravan 在您更新解决方案之前发布了一个适用于我的解决方案。我至少想为你的答案投票给你。我相信其他人也会觉得它很有帮助。谢谢!
【解决方案2】:

试试这个

// 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);

【讨论】:

  • 感谢您发布 Shravan,但不推荐使用 manage_{$taxonomy}_custom_column。这就是我发帖的原因。
  • Opps,但它在我最新的 wordpress(4.4.2) 上运行良好,没有任何警告。
  • 我只是把你的代码放进去,它确实有效!您的代码与我一直使用的代码之间的区别在于最后的10, 3。那个有什么用途?优先级?
  • 作为变量名,它显示当前的 texanomy id。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-04
  • 1970-01-01
  • 2015-10-01
  • 1970-01-01
  • 2015-02-16
  • 2011-11-19
  • 2016-11-29
相关资源
最近更新 更多