【问题标题】:Displayin custom taxonomies in custom post type columns在自定义帖子类型列中显示自定义分类
【发布时间】:2014-11-04 18:57:45
【问题描述】:

我查看了几篇关于向 CPT 列添加自定义分类法的帖子;除了实际显示所述分类法(出版物)之外,我能够让一切正常工作。这是我的 CPT 代码:

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

自定义分类法

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

现在我看到了两种不同的选择,或者尝试在分类参数中添加 show_ui/show_admin_column,或者使用带有 switch 语句的另一个函数。我在下面都试过了,有什么重要的我遗漏了吗?

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

【问题讨论】:

    标签: php wordpress custom-post-type taxonomy


    【解决方案1】:

    删除您的自定义列函数,并将'show_admin_column' =&gt; true 添加到register_taxonomy 的实际参数数组中:

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

    编辑:您可能还想将 'taxonomies' =&gt; array( 'publication' ) 添加到 register_post_type 参数中。

    【讨论】:

    • register_post_type( 'press', 'taxonomies' =&gt; array( 'publication' ), $args ); 抛出“意外双箭头”错误。
    • 那个键值对应该是@​​987654328@的一部分
    • 这就是整个事情的样子吗?我忽略了什么? pastebin.com/k3WQZ8mc
    • 看起来不错,只需删除第 35 行的 , $args 即可,这会引发通知。为新闻帖子分配术语后,发布标签将立即显示在列中。
    • 它甚至在我删除 , $args 之前就神奇地出现了。非常感谢。我即将尝试将特色图片添加到自定义列中,所以我会回来的。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 2012-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多