2

我需要为 2 种不同的帖子类型注册分类法,并且我尝试使用以下代码来实现这一点:

功能 add_custom_taxonomies() {

        // Add new taxonomy, make it hierarchical (like categories)
          $labels_adp = array(
            'name'                => _x( 'Apples', 'taxonomy general name' ),
            'singular_name'       => _x( 'Apple', 'taxonomy singular name' ),
            'search_items'        => __( 'Search Apples' ),
            'all_items'           => __( 'All Apples' ),
            'parent_item'         => __( 'Parent Apples' ),
            'parent_item_colon'   => __( 'Parent Apples:' ),
            'edit_item'           => __( 'Edit Apples' ), 
            'update_item'         => __( 'Update Apples' ),
            'add_new_item'        => __( 'Add New Apples' ),
            'new_item_name'       => __( 'New Genre Apples' ),
            'menu_name'           => __( 'Apples' )
          );    

          $args_adp = array(
            'hierarchical'        => true,
            'labels'              => $labels_adp,
            'show_ui'             => true,
            'show_admin_column'   => true,
            'query_var'           => true,
            'rewrite'             => array( 'slug' => 'apple' )
          );
          $labels_cw = array(
            'name'                => _x( 'Genres', 'taxonomy general name' ),
            'singular_name'       => _x( 'Genre', 'taxonomy singular name' ),
            'search_items'        => __( 'Search Genres' ),
            'all_items'           => __( 'All Genres' ),
            'parent_item'         => __( 'Parent Genre' ),
            'parent_item_colon'   => __( 'Parent Genre:' ),
            'edit_item'           => __( 'Edit Genre' ), 
            'update_item'         => __( 'Update Genre' ),
            'add_new_item'        => __( 'Add New Genre' ),
            'new_item_name'       => __( 'New Genre Name' ),
            'menu_name'           => __( 'Genre' )
          );    

          $args_cw = array(
            'hierarchical'        => true,
            'labels'              => $labels_cw,
            'show_ui'             => true,
            'show_admin_column'   => true,
            'query_var'           => true,
            'rewrite'             => array( 'slug' => 'genre' )
          );

          register_taxonomy( 'apple', array( 'adp' ), $args_adp );
          register_taxonomy( 'genre', array( 'cw' ), $args_cw );

    }
    add_action( 'init', 'add_custom_taxonomies', 0 );

不幸的是,它根本不起作用,只呈现一种分类法一种帖子类型。不知道为什么。

4

2 回答 2

4

您可以在定义分类名称后立即使用数组:

register_taxonomy(
  'movie_name',
   array( 'reviews','news' ), 
  ..etc.. 
  )

http://wordpress.org/support/topic/one-taxonomy-for-multiple-post-types

于 2014-08-05T14:12:56.733 回答
0

如果您尝试为不同的帖子类型注册个人分类法,请制作两个单独的功能来注册您的帖子

function add_custom_taxonomies() {

            // Add new taxonomy, make it hierarchical (like categories)
             $labels_cw = array(
        'name'                => _x( 'Genres', 'taxonomy general name' ),
        'singular_name'       => _x( 'Genre', 'taxonomy singular name' ),
        'search_items'        => __( 'Search Genres' ),
        'all_items'           => __( 'All Genres' ),
        'parent_item'         => __( 'Parent Genre' ),
        'parent_item_colon'   => __( 'Parent Genre:' ),
        'edit_item'           => __( 'Edit Genre' ), 
        'update_item'         => __( 'Update Genre' ),
        'add_new_item'        => __( 'Add New Genre' ),
        'new_item_name'       => __( 'New Genre Name' ),
        'menu_name'           => __( 'Genre' )
      );    

              $args_cw = array(
                'hierarchical'        => true,
                'labels'              => $labels_cw,
                'show_ui'             => true,
                'show_admin_column'   => true,
                'query_var'           => true,
                'rewrite'             => array( 'slug' => 'genre' )
              );

                      register_taxonomy( 'genre', array( 'cw' ), $args_cw );


        }
        add_action( 'init', 'add_custom_taxonomies', 0 );
function add_custom_taxonomies1() {

            // Add new taxonomy, make it hierarchical (like categories)
               $labels_adp = array(
        'name'                => _x( 'Apples', 'taxonomy general name' ),
        'singular_name'       => _x( 'Apple', 'taxonomy singular name' ),
        'search_items'        => __( 'Search Apples' ),
        'all_items'           => __( 'All Apples' ),
        'parent_item'         => __( 'Parent Apples' ),
        'parent_item_colon'   => __( 'Parent Apples:' ),
        'edit_item'           => __( 'Edit Apples' ), 
        'update_item'         => __( 'Update Apples' ),
        'add_new_item'        => __( 'Add New Apples' ),
        'new_item_name'       => __( 'New Genre Apples' ),
        'menu_name'           => __( 'Apples' )
      );    

              $args_adp = array(
                'hierarchical'        => true,
                'labels'              => $labels_adp,
                'show_ui'             => true,
                'show_admin_column'   => true,
                'query_var'           => true,
                'rewrite'             => array( 'slug' => 'apple' )
              );

                      register_taxonomy( 'genre', array( 'adp' ), $args_adp );


        }
        add_action( 'init', 'add_custom_taxonomies1', 0 );

在数组('adp','cw')中,adp 和 cw 是 post 类型

于 2013-04-11T03:51:12.797 回答