关于范畴的基本CPT问题

时间:2021-05-28 作者:markw

这里缺少一些基本的东西。这是我的CPT代码。。。

// Create a custom post type
//
add_action( \'init\', \'custom_post_type_func\' );
function custom_post_type_func() {
    //posttypename = PDSH Posts
    $labels = array (
    \'name\' => _x( \'PDSH Posts\', \'pdsh_posts\' ),
    \'singular_name\' => _x( \'PDSH Post\', \'pdsh_posts\' ),
    \'add_new\' => _x( \'Add New\', \'pdsh_posts\' ),
    \'add_new_item\' => _x( \'Add New PDSH Post\', \'pdsh_posts\' ),
    \'edit_item\' => _x( \'Edit PDSH Post\', \'pdsh_posts\' ),
    \'new_item\' => _x( \'New PDSH Post\', \'pdsh_posts\' ),
    \'view_item\' => _x( \'View PDSH Post\', \'pdsh_posts\' ),
    \'search_items\' => _x( \'Search PDSH Posts\', \'pdsh_posts\' ),
    \'not_found\' => _x( \'No PDSH Posts found\', \'pdsh_posts\' ),
    \'not_found_in_trash\' => _x( \'No PDSH Posts found in Trash\', \'pdsh_posts\' ),
    \'parent_item_colon\' => _x( \'Parent PDSH Post:\', \'pdsh_posts\' ),
    \'menu_name\' => _x( \'PDSH Posts\', \'pdsh_posts\' ),
    );
    $args = array (
    \'labels\' => $labels,
    \'hierarchical\' => true,
    \'description\' => \'Hi, this is my custom post type.\',
    \'supports\' => array( \'title\', \'editor\', \'excerpt\', \'author\', \'thumbnail\', \'trackbacks\', \'custom-fields\', \'comments\', \'revisions\', \'page-attributes\' ),
    \'taxonomies\' => array( \'category\', \'post_tag\', \'page-category\' ),
    \'public\' => true,
    \'show_ui\' => true,
    \'show_in_menu\' => true,
    \'show_in_nav_menus\' => true,
    \'publicly_queryable\' => true,
    \'exclude_from_search\' => false,
    \'has_archive\' => true,
    \'query_var\' => true,
    \'can_export\' => true,
    \'rewrite\' => true,
    \'capability_type\' => \'post\'
    );
    register_post_type( \'pdsh_posts\', $args );
}
效果很好!我正在使用WP All Import插件创建3378篇PDSH帖子,其中包含三个父类别:字母、创建者、道德。他们在这里看起来都很正常。。。enter image description here但当我查看其中一个类别时,归档页面无法看到帖子,转到“无内容”,抛出一个“未找到”。我是否需要以某种方式注册所有106个类别,或者只注册三个父类别?

更新日期:

以下是我的注册尝试;按字母顺序排列;和;“A”;。。。

//
// Hook into the init action and call create_book_taxonomies when it fires
// 
add_action( \'init\', \'create_post_types_hierarchical_taxonomy\', 0 );
// Create a custom taxonomy name it subjects for your posts
function create_post_types_hierarchical_taxonomy() {
    // Add new taxonomy, make it hierarchical like categories
    // //first do the translations part for GUI
  $labels = array(
    \'name\' => _x( \'Alphabetical\', \'taxonomy general name\' ),
    \'singular_name\' => _x( \'Alphabetical\', \'taxonomy singular name\' ),
    \'search_items\' =>  __( \'Search Alphabetical\' ),
    \'all_items\' => __( \'All Alphabetical\' ),
    \'parent_item\' => __( \'Parent Alphabetical\' ),
    \'parent_item_colon\' => __( \'Parent Alphabetical:\' ),
    \'edit_item\' => __( \'Edit Alphabetical\' ), 
    \'update_item\' => __( \'Update Alphabetical\' ),
    \'add_new_item\' => __( \'Add New Alphabetical\' ),
    \'new_item_name\' => __( \'New Alphabetical Name\' ),
    \'menu_name\' => __( \'Alphabetical\' ),
  );
    // Now register the taxonomy
    register_taxonomy(\'Alphabetical\',array(\'A\'), array(
        \'hierarchical\' => true,
        \'labels\' => $labels,
        \'show_ui\' => true,
        \'show_in_rest\' => true,
        \'show_admin_column\' => true,
        \'query_var\' => true,
        \'rewrite\' => array( \'slug\' => \'Alphabetical\' ),
    ));
}

1 个回复
最合适的回答,由SO网友:Antti Koskinen 整理而成

也许现在已经很晚了,但是register_taxonomy 打错电话了。第二个参数不应该是为其注册分类的帖子类型吗?一、 e。array(\'pdsh_posts\').

是的,您需要为post类型分别注册三个分类法中的每一个

参见法典示例,https://developer.wordpress.org/reference/functions/register_taxonomy/#comment-397

Edit

实际上,这可能不是我上面写的。使用默认值注册cptcategory 如果您想使用分类法,那么应该只需要cpt的以下参数

\'taxonomies\' => array( \'category\' ),

在类别视图中找不到您的原因是,默认情况下,循环只查找帖子,而不是您的CPT。您需要使用将cpt添加到查询中pre_get_posts

此处示例,https://wordpress.stackexchange.com/a/114853/144392

相关推荐