douan9541 2014-11-04 18:57
浏览 36
已采纳

在自定义帖子类型列中显示自定义分类

I've looked over several posts about adding custom taxonomies to CPT columns; I'm able to get everything working except actually displaying said taxonomies (publication). Here's my CPT code:

add_action( 'init', 'pb_custom_post_type' );
function pb_custom_post_type() {
  $labels = array(
  'name'               => _x( 'Press', 'post type general name' ),
  'singular_name'      => _x( 'Press', 'post type singular name' ),
  'add_new'            => _x( 'Add New', 'review' ),
  'add_new_item'       => __( 'Add New Press' ),
  'edit_item'          => __( 'Edit Press' ),
  'new_item'           => __( 'New Press' ),
  'all_items'          => __( 'All Press' ),
  'view_item'          => __( 'View Press' ),
  'search_items'       => __( 'Search Press' ),
  'not_found'          => __( 'No press found' ),
  'not_found_in_trash' => __( 'No press found in the Trash' ), 
  'parent_item_colon'  => '',
  'menu_name'          => 'Press'
);
  $args = array(
  'labels'        => $labels,
  'description'   => 'Press information',
  'public'        => true,
  'menu_position' => 20,
  'supports'      => array( 'title', 'editor', 'thumbnail' ),
  'has_archive'   => true,
);
  register_post_type( 'press', $args ); 
}

add_filter( 'manage_edit-press_columns', 'my_edit_press_columns' ) ;
function my_edit_press_columns( $columns ) {
  $columns = array(
    'cb' => '<input type="checkbox" />',
    'title' => __( 'Title' ),
    'publication' => __( 'Publication' ),
    'date' => __( 'Date' )
  );

  return $columns;
}

Custom Taxonomy

add_action( 'init', 'create_my_taxonomies', 0 );
function create_my_taxonomies() {
    register_taxonomy( 'publication', 'press', array( 'hierarchical' => false, 'label' => 'Publications', 'query_var' => false, 'rewrite' => true ) );
}

Now I've seen two different choices, either trying to add show_ui/show_admin_column in the taxonomy arguments, or having another function with switch statements. I've tried both below, is there something crucial I'm missing?

1

add_action( 'init', 'create_my_taxonomies', 0 );
function create_my_taxonomies() {
  $args = array (   
    'show_ui'           => true,
    'show_admin_column' => true,
  );
   register_taxonomy( 'publication', 'press', array( 'hierarchical' => false, 'label' => 'Publications', 'query_var' => false, 'rewrite' => true ), $args );
}

2

function custom_columns( $column, $post_id ) {
  switch ( $column ) {
    case "publication":
      echo get_post_meta( $post_id, 'publication', true);
      break;
  }
}
add_action( "manage_posts_custom_column", "custom_columns", 10, 2 );
  • 写回答

1条回答 默认 最新

  • dseslyh6662605 2014-11-04 19:13
    关注

    Remove your custom columns function, and add 'show_admin_column' => true to the actual array of args for register_taxonomy:

    register_taxonomy( 'publication', 'press', array( 'show_admin_column' => true, 'hierarchical' => false, 'label' => 'Publications', 'query_var' => false, 'rewrite' => true ) );
    

    Edit: you might also want to add 'taxonomies' => array( 'publication' ) to the register_post_type args.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败