Wordpress无法为自定义帖子类型添加类别

时间:2017-08-29 21:32:13

标签: php wordpress custom-post-type categories

我在Wordpress 4.8.1上。出于某种原因,当我注册自定义帖子类型时,我添加:

'taxonomies' => ['category'],

它不起作用。类别不会显示在自定义帖子类型编辑屏幕上。它之前总是有效,所以我不明白为什么它现在不起作用。它甚至在几个月前在该网站上工作。有人帮忙:))

!! 更新 !!

这是我的代码,我意识到这是必要的lol

function spanish_cpt() {
  $labels = array(
    'name'               => _x( 'Spanish', 'post type general name' ),
    'singular_name'      => _x( 'Spanish Post', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Spanish Post' ),
    'edit_item'          => __( 'Edit Spanish Post' ),
    'new_item'           => __( 'New Spanish Post' ),
    'all_items'          => __( 'All Spanish Posts' ),
    'view_item'          => __( 'View Spanish Post' ),
    'search_items'       => __( 'Search Spanish Posts' ),
    'not_found'          => __( 'No spanish posts found' ),
    'not_found_in_trash' => __( 'No spanish posts found in the Trash' ), 
    'parent_item_colon'  => '',
    'menu_name'          => 'Spanish'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Spanish version of posts',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array('title', 'editor', 'thumbnail',
                             'author', 'comments', 'revisions'),
    'has_archive'   => 'es',
    'taxonomies' => array('category', 'post_tag'),
  );
  register_post_type( 'spanish', $args ); 
}
add_action( 'init', 'spanish_cpt' );

2 个答案:

答案 0 :(得分:0)

functions.php

中使用此功能

function register_results(){

$labels = array(
    'name' => _x('Results', 'post type general name'),
    'singular_name' => _x('Results', 'post type singular name'),
    'add_new' => _x('Add New', 'Results'),
    'add_new_item' => __('Add New Case Results'),
    'edit_item' => __('Edit Case Results'),
    'new_item' => __('New Case Results'),
    'all_items' => __('All Case Results'),
    'view_item' => __('View Case Results'),
    'search_items' => __('Search Case Results'),
    'not_found' => __('No Results found'),
    'not_found_in_trash' => __('No Results found in the Trash'),
    'menu_name' => 'Case Results'
);
$args = array(
    'labels' => $labels,
    'description' => 'Results and Results Related information will be hold on this',
    'public' => true,
    'show_in_menu' => true,
    'menu' => 5,
    'menu_icon'=>'dashicons-admin-post',
    'supports' => array('title', 'editor', 'thumbnail', 'post-format', 'excerpt'),
    'has_archive' => true,
    'show_in_nav_menus' => true,
    'show_ui' => true,
    'taxonomies' => array('Category_results')
);
register_taxonomy(
    'Category_results', 'results', array(
    'hierarchical' => true,
    'label' => 'Custom Category',
    'show_admin_column' => true,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => true
));
register_post_type('results', $args);

}

add_action('init','register_results');

答案 1 :(得分:0)

我发现了问题所在。我使用自定义的帖子类型帮助程序类,以便以更清洁的方式生成它们。原始代码是这个

$spanish = new CPT([
    'post_type_name' => 'spanish',
    'singular' => 'Spanish Post',
    'plural' => 'Spanish Posts',
    'slug' => 'es'
], [
    'public' => true,
    'menu_icon' => 'dashicons-admin-post',
    'query_var' => true,
    'has_archive' => 'es',
    'hierarchical' => true,
    'can_export' => true,
    'menu_position' => 5,
    'publicly_queryable' => true,
    'rewrite' => [
        'slug' => 'es/%spanish_categories%', 
        'with_front' => false
    ],
    'supports' => [
        'title', 'editor', 'thumbnail',
        'author', 'comments', 'revisions',
    ],
    'taxonomies' => ['category'],
]);

这一直有效,但我想当我更新到4.8的东西破了,它不再有效。所以我只是切换到创建自定义帖子类型的常规方式,现在可以使用了。感谢大家的帮助和参与。