Taxonomy.php不适用于自定义帖子类型

问题描述

我使用名称服务创建了一个自定义帖子类型,并使用名称catservice添加分类法。我将分类文件命名为taxonomy-catservice.PHP,但无法正常工作,我还通过将其重命名为taxonomy-services进行了尝试,但它也无法正常工作。我该怎么办?

解决方法

不知道邮编和自定义分类法中的代码就很难分辨。 但是,我能给您的最好建议是在创建代码的地方检查custom_post代码。 并且看到您已经包括了您的自定义帖子类型已经包括了您的分类法。 例如,请参见以下代码:

$args = array(
            'label'                 => __( 'CUstom post type','domain' ),'description'           => __( 'description','labels'                => $labels,'supports'              => array( 'title','editor','thumbnail','trackbacks','revisions','custom-fields','page-attributes','post-formats' ),'taxonomies'            => array( 'custom_taxonomy'),'hierarchical'          => true,'public'                => true,'show_ui'               => true,'show_in_menu'          => true,'menu_position'         => 5,'show_in_admin_bar'     => true,'show_in_nav_menus'     => true,'can_export'            => true,'has_archive'           => false,'exclude_from_search'   => false,'publicly_queryable'    => true,'rewrite'               => false,'capability_type'       => 'page',);
        register_post_type( 'custom_post_type',$args );

重要的是要检查键taxonomies是否存在以及您自定义的分类名称是否存在。

那样,或者当您这样注册时,也可以从自定义分类中进行操作:

register_taxonomy( 'custom_taxonomy',array( 'custom_post_type_name' ),$args );

您会看到该函数中的第二个参数获取了一个带有自定义帖子类型名称的数组。

希望这会给您一些帮助:)

编辑:

使用该插件运行测试之后,因此在创建“自定义”帖子类型后创建“自定义分类”时,我可以选择应与“自定义分类”链接的帖子类型。参见下图:

enter image description here

这对我有用,因此现在可以显示分类法,并可以添加到我的自定义帖子类型中

,

尝试使用此代码,这对我有帮助,在function.php中添加代码后,还要更新永久链接。

$labels = array(
        'name'                  => _x( 'Services','Post type general name','textdomain' ),'singular_name'         => _x( 'Service','Post type singular name','menu_name'             => _x( 'Services','Admin Menu text','name_admin_bar'        => _x( 'Service','Add New on Toolbar','add_new'               => __( 'Add New','add_new_item'          => __( 'Add New Service','new_item'              => __( 'New Service','edit_item'             => __( 'Edit Service','view_item'             => __( 'View Service','all_items'             => __( 'All Services','search_items'          => __( 'Search Services','parent_item_colon'     => __( 'Parent Services:','not_found'             => __( 'No services found.','not_found_in_trash'    => __( 'No services found in Trash.','featured_image'        => _x( 'Service Cover Image','Overrides the “Featured Image” phrase for this post type. Added in 4.3','set_featured_image'    => _x( 'Set cover image','Overrides the “Set featured image” phrase for this post type. Added in 4.3','remove_featured_image' => _x( 'Remove cover image','Overrides the “Remove featured image” phrase for this post type. Added in 4.3','use_featured_image'    => _x( 'Use as cover image','Overrides the “Use as featured image” phrase for this post type. Added in 4.3','archives'              => _x( 'Service archives','The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4','insert_into_item'      => _x( 'Insert into services','Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4','uploaded_to_this_item' => _x( 'Uploaded to this services','Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4','filter_items_list'     => _x( 'Filter services list','Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4','items_list_navigation' => _x( 'Services list navigation','Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4','items_list'            => _x( 'Services list','Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4',);

    
    $args = array(
        'label'                 => __( 'Services','taxonomies'            => array( 'catservice'),);
    register_post_type( 'services',$args );
 
    // Add new taxonomy,NOT hierarchical (like tags)
    $labels = array(
        'name'                       => _x( 'Service Categories','taxonomy general name','singular_name'              => _x( 'Service Category','taxonomy singular name','search_items'               => __( 'Search Service Categories','popular_items'              => __( 'Popular Service Categories','all_items'                  => __( 'All Service Categories','parent_item'                => null,'parent_item_colon'          => null,'edit_item'                  => __( 'Edit Service Category','update_item'                => __( 'Update Service Category','add_new_item'               => __( 'Add New Service Category','new_item_name'              => __( 'New Service Category Name','separate_items_with_commas' => __( 'Separate service categories with commas','add_or_remove_items'        => __( 'Add or remove service categories','choose_from_most_used'      => __( 'Choose from the most used service categories','not_found'                  => __( 'No service categories found.','menu_name'                  => __( 'Service Categories',);
 
    $args = array(
        'hierarchical'          => false,'show_admin_column'     => true,'update_count_callback' => '_update_post_term_count','query_var'             => true,'rewrite'               => array( 'slug' => 'catservice' ),);

    register_taxonomy( 'catservice',array( 'services' ),$args );

注意:从设置选项更新永久链接。