无法创建新的分类

时间:2018-09-07 作者:Alaksandar Jesus Gene

我正在关注这个link 并将下面的代码复制粘贴到我的函数中。php文件

<?php
// hook into the init action and call create_book_taxonomies when it fires
add_action( \'init\', \'create_book_taxonomies\' );

// create two taxonomies, genres and writers for the post type "book"
function create_book_taxonomies() {
    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
        \'name\'              => _x( \'Genres\', \'taxonomy general name\', \'textdomain\' ),
        \'singular_name\'     => _x( \'Genre\', \'taxonomy singular name\', \'textdomain\' ),
        \'search_items\'      => __( \'Search Genres\', \'textdomain\' ),
        \'all_items\'         => __( \'All Genres\', \'textdomain\' ),
        \'parent_item\'       => __( \'Parent Genre\', \'textdomain\' ),
        \'parent_item_colon\' => __( \'Parent Genre:\', \'textdomain\' ),
        \'edit_item\'         => __( \'Edit Genre\', \'textdomain\' ),
        \'update_item\'       => __( \'Update Genre\', \'textdomain\' ),
        \'add_new_item\'      => __( \'Add New Genre\', \'textdomain\' ),
        \'new_item_name\'     => __( \'New Genre Name\', \'textdomain\' ),
        \'menu_name\'         => __( \'Genre\', \'textdomain\' ),
    );

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

    register_taxonomy( \'genre\', array( \'book\' ), $args );

    // Add new taxonomy, NOT hierarchical (like tags)
    $labels = array(
        \'name\'                       => _x( \'Writers\', \'taxonomy general name\', \'textdomain\' ),
        \'singular_name\'              => _x( \'Writer\', \'taxonomy singular name\', \'textdomain\' ),
        \'search_items\'               => __( \'Search Writers\', \'textdomain\' ),
        \'popular_items\'              => __( \'Popular Writers\', \'textdomain\' ),
        \'all_items\'                  => __( \'All Writers\', \'textdomain\' ),
        \'parent_item\'                => null,
        \'parent_item_colon\'          => null,
        \'edit_item\'                  => __( \'Edit Writer\', \'textdomain\' ),
        \'update_item\'                => __( \'Update Writer\', \'textdomain\' ),
        \'add_new_item\'               => __( \'Add New Writer\', \'textdomain\' ),
        \'new_item_name\'              => __( \'New Writer Name\', \'textdomain\' ),
        \'separate_items_with_commas\' => __( \'Separate writers with commas\', \'textdomain\' ),
        \'add_or_remove_items\'        => __( \'Add or remove writers\', \'textdomain\' ),
        \'choose_from_most_used\'      => __( \'Choose from the most used writers\', \'textdomain\' ),
        \'not_found\'                  => __( \'No writers found.\', \'textdomain\' ),
        \'menu_name\'                  => __( \'Writers\', \'textdomain\' ),
    );

    $args = array(
        \'hierarchical\'          => false,
        \'labels\'                => $labels,
        \'show_ui\'               => true,
        \'show_admin_column\'     => true,
        \'update_count_callback\' => \'_update_post_term_count\',
        \'query_var\'             => true,
        \'rewrite\'               => array( \'slug\' => \'writer\' ),
    );

    register_taxonomy( \'writer\', \'book\', $args );
}
?>
但是我没有看到在我的wp管理面板中创建的分类法。我在4.9.8版上,有什么我遗漏的吗。

EDIT 1我复制粘贴了代码,但仍然没有看到任何分类法。据我所知,init函数没有被调用。。。

例如,我在函数前后都尝试了回声

function wpdocs_codex_book_init() {
echo "<h1>Book</h1>"; //I dont see the Book text in h1 tags in admin panel


echo "<h1>Book</h1>"; //I see the Book text in h1 tags in admin panel
function wpdocs_codex_book_init() {

2 个回复
最合适的回答,由SO网友:maverick 整理而成

使用条件标记post\\u type\\u exists()检查post type book是否存在,如果存在,则向其添加分类法,否则不存在

<?php

if( post_type_exists( \'book\' ){
add_action( \'init\', \'create_book_taxonomies\' );

// create two taxonomies, genres and writers for the post type "book"
function create_book_taxonomies() {
    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
        \'name\'              => _x( \'Genres\', \'taxonomy general name\', \'textdomain\' ),
        \'singular_name\'     => _x( \'Genre\', \'taxonomy singular name\', \'textdomain\' ),
        \'search_items\'      => __( \'Search Genres\', \'textdomain\' ),
        \'all_items\'         => __( \'All Genres\', \'textdomain\' ),
        \'parent_item\'       => __( \'Parent Genre\', \'textdomain\' ),
        \'parent_item_colon\' => __( \'Parent Genre:\', \'textdomain\' ),
        \'edit_item\'         => __( \'Edit Genre\', \'textdomain\' ),
        \'update_item\'       => __( \'Update Genre\', \'textdomain\' ),
        \'add_new_item\'      => __( \'Add New Genre\', \'textdomain\' ),
        \'new_item_name\'     => __( \'New Genre Name\', \'textdomain\' ),
        \'menu_name\'         => __( \'Genre\', \'textdomain\' ),
    );

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

    register_taxonomy( \'genre\', array( \'book\' ), $args );

    // Add new taxonomy, NOT hierarchical (like tags)
    $labels = array(
        \'name\'                       => _x( \'Writers\', \'taxonomy general name\', \'textdomain\' ),
        \'singular_name\'              => _x( \'Writer\', \'taxonomy singular name\', \'textdomain\' ),
        \'search_items\'               => __( \'Search Writers\', \'textdomain\' ),
        \'popular_items\'              => __( \'Popular Writers\', \'textdomain\' ),
        \'all_items\'                  => __( \'All Writers\', \'textdomain\' ),
        \'parent_item\'                => null,
        \'parent_item_colon\'          => null,
        \'edit_item\'                  => __( \'Edit Writer\', \'textdomain\' ),
        \'update_item\'                => __( \'Update Writer\', \'textdomain\' ),
        \'add_new_item\'               => __( \'Add New Writer\', \'textdomain\' ),
        \'new_item_name\'              => __( \'New Writer Name\', \'textdomain\' ),
        \'separate_items_with_commas\' => __( \'Separate writers with commas\', \'textdomain\' ),
        \'add_or_remove_items\'        => __( \'Add or remove writers\', \'textdomain\' ),
        \'choose_from_most_used\'      => __( \'Choose from the most used writers\', \'textdomain\' ),
        \'not_found\'                  => __( \'No writers found.\', \'textdomain\' ),
        \'menu_name\'                  => __( \'Writers\', \'textdomain\' ),
    );

    $args = array(
        \'hierarchical\'          => false,
        \'labels\'                => $labels,
        \'show_ui\'               => true,
        \'show_admin_column\'     => true,
        \'update_count_callback\' => \'_update_post_term_count\',
        \'query_var\'             => true,
        \'rewrite\'               => array( \'slug\' => \'writer\' ),
    );

    register_taxonomy( \'writer\', \'book\', $args );
}

}
或者,如果您尚未创建post type book,您可以这样做:

/**
 * Register a custom post type called "book".
 *
 * @see get_post_type_labels() for label keys.
 */
function wpdocs_codex_book_init() {
    $labels = array(
        \'name\'                  => _x( \'Books\', \'Post type general name\', \'textdomain\' ),
        \'singular_name\'         => _x( \'Book\', \'Post type singular name\', \'textdomain\' ),
        \'menu_name\'             => _x( \'Books\', \'Admin Menu text\', \'textdomain\' ),
        \'name_admin_bar\'        => _x( \'Book\', \'Add New on Toolbar\', \'textdomain\' ),
        \'add_new\'               => __( \'Add New\', \'textdomain\' ),
        \'add_new_item\'          => __( \'Add New Book\', \'textdomain\' ),
        \'new_item\'              => __( \'New Book\', \'textdomain\' ),
        \'edit_item\'             => __( \'Edit Book\', \'textdomain\' ),
        \'view_item\'             => __( \'View Book\', \'textdomain\' ),
        \'all_items\'             => __( \'All Books\', \'textdomain\' ),
        \'search_items\'          => __( \'Search Books\', \'textdomain\' ),
        \'parent_item_colon\'     => __( \'Parent Books:\', \'textdomain\' ),
        \'not_found\'             => __( \'No books found.\', \'textdomain\' ),
        \'not_found_in_trash\'    => __( \'No books found in Trash.\', \'textdomain\' ),
        \'featured_image\'        => _x( \'Book Cover Image\', \'Overrides the “Featured Image” phrase for this post type. Added in 4.3\', \'textdomain\' ),
        \'set_featured_image\'    => _x( \'Set cover image\', \'Overrides the “Set featured image” phrase for this post type. Added in 4.3\', \'textdomain\' ),
        \'remove_featured_image\' => _x( \'Remove cover image\', \'Overrides the “Remove featured image” phrase for this post type. Added in 4.3\', \'textdomain\' ),
        \'use_featured_image\'    => _x( \'Use as cover image\', \'Overrides the “Use as featured image” phrase for this post type. Added in 4.3\', \'textdomain\' ),
        \'archives\'              => _x( \'Book archives\', \'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4\', \'textdomain\' ),
        \'insert_into_item\'      => _x( \'Insert into book\', \'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4\', \'textdomain\' ),
        \'uploaded_to_this_item\' => _x( \'Uploaded to this book\', \'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4\', \'textdomain\' ),
        \'filter_items_list\'     => _x( \'Filter books 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\', \'textdomain\' ),
        \'items_list_navigation\' => _x( \'Books 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\', \'textdomain\' ),
        \'items_list\'            => _x( \'Books list\', \'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4\', \'textdomain\' ),
    );

    $args = array(
        \'labels\'             => $labels,
        \'public\'             => true,
        \'publicly_queryable\' => true,
        \'show_ui\'            => true,
        \'show_in_menu\'       => true,
        \'query_var\'          => true,
        \'rewrite\'            => array( \'slug\' => \'book\' ),
        \'capability_type\'    => \'post\',
        \'has_archive\'        => true,
        \'hierarchical\'       => false,
        \'menu_position\'      => null,
        \'supports\'           => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\' ),
    );

    register_post_type( \'book\', $args );
}

add_action( \'init\', \'wpdocs_codex_book_init\' );

add_action( \'init\', \'create_book_taxonomies\' );

// create two taxonomies, genres and writers for the post type "book"
function create_book_taxonomies() {
    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
        \'name\'              => _x( \'Genres\', \'taxonomy general name\', \'textdomain\' ),
        \'singular_name\'     => _x( \'Genre\', \'taxonomy singular name\', \'textdomain\' ),
        \'search_items\'      => __( \'Search Genres\', \'textdomain\' ),
        \'all_items\'         => __( \'All Genres\', \'textdomain\' ),
        \'parent_item\'       => __( \'Parent Genre\', \'textdomain\' ),
        \'parent_item_colon\' => __( \'Parent Genre:\', \'textdomain\' ),
        \'edit_item\'         => __( \'Edit Genre\', \'textdomain\' ),
        \'update_item\'       => __( \'Update Genre\', \'textdomain\' ),
        \'add_new_item\'      => __( \'Add New Genre\', \'textdomain\' ),
        \'new_item_name\'     => __( \'New Genre Name\', \'textdomain\' ),
        \'menu_name\'         => __( \'Genre\', \'textdomain\' ),
    );

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

    register_taxonomy( \'genre\', array( \'book\' ), $args );

    // Add new taxonomy, NOT hierarchical (like tags)
    $labels = array(
        \'name\'                       => _x( \'Writers\', \'taxonomy general name\', \'textdomain\' ),
        \'singular_name\'              => _x( \'Writer\', \'taxonomy singular name\', \'textdomain\' ),
        \'search_items\'               => __( \'Search Writers\', \'textdomain\' ),
        \'popular_items\'              => __( \'Popular Writers\', \'textdomain\' ),
        \'all_items\'                  => __( \'All Writers\', \'textdomain\' ),
        \'parent_item\'                => null,
        \'parent_item_colon\'          => null,
        \'edit_item\'                  => __( \'Edit Writer\', \'textdomain\' ),
        \'update_item\'                => __( \'Update Writer\', \'textdomain\' ),
        \'add_new_item\'               => __( \'Add New Writer\', \'textdomain\' ),
        \'new_item_name\'              => __( \'New Writer Name\', \'textdomain\' ),
        \'separate_items_with_commas\' => __( \'Separate writers with commas\', \'textdomain\' ),
        \'add_or_remove_items\'        => __( \'Add or remove writers\', \'textdomain\' ),
        \'choose_from_most_used\'      => __( \'Choose from the most used writers\', \'textdomain\' ),
        \'not_found\'                  => __( \'No writers found.\', \'textdomain\' ),
        \'menu_name\'                  => __( \'Writers\', \'textdomain\' ),
    );

    $args = array(
        \'hierarchical\'          => false,
        \'labels\'                => $labels,
        \'show_ui\'               => true,
        \'show_admin_column\'     => true,
        \'update_count_callback\' => \'_update_post_term_count\',
        \'query_var\'             => true,
        \'rewrite\'               => array( \'slug\' => \'writer\' ),
    );
 register_taxonomy( \'writer\', \'book\', $args );

// Following code will assign default taxonomy to genre taxonomy
    if( !term_exists( \'intelligence\', \'genre\' ) ){
    $parent_term = term_exists( \'genre\', \'genre\' ); 
     wp_insert_term(
       \'Intelligence\',
       \'genre\',
        array(
           \'description\'=> \'Book about Intelligence\',
           \'slug\' => \'intelligence\',
           \'parent\'=> $parent_term[\'term_id\']
        )
    );
  }
}
我已经测试了这段代码,它可以正常工作,应该也可以正常工作

如果要将自定义分类添加到wordpress帖子,请使用:

register_taxonomy( \'writer\', array( \'book\', \'post\' ), $args );

SO网友:Niket Joshi

最近,我为我的项目创建了分类法,它非常适合我的需求,我在这里添加了我的测试代码和工作代码。

//registration of taxonomies

function my_taxonomies_hazard() {

    //labels array

$labels = array(
    \'name\'              => _x( \'Hazard Categories\', \'taxonomy general name\' ),
    \'singular_name\'     => _x( \'Hazard  Category\', \'taxonomy singular name\' ),
    \'search_items\'      => __( \'Search Hazard Categories\' ),
    \'all_items\'         => __( \'All Hazard Categories\' ),
    \'parent_item\'       => __( \'Parent Hazard Category\' ),
    \'parent_item_colon\' => __( \'Parent Hazard Category:\' ),
    \'edit_item\'         => __( \'Edit Hazard Category\' ),
    \'update_item\'       => __( \'Update Hazard Category\' ),
    \'add_new_item\'      => __( \'Add New Hazard Category\' ),
    \'new_item_name\'     => __( \'New Hazard Category\' ),
    \'menu_name\'         => __( \'Hazard Categories\' ),
  );

   //args array

$args = array(
    \'labels\' => $labels,
    \'hierarchical\' => true,
  );

  register_taxonomy( \'hazard_category\', \'hazard\', $args );
}

add_action( \'init\', \'my_taxonomies_hazard\', 0 );
这段代码非常适合我,如果有任何问题,请随时发表评论。

结束

相关推荐

If Custom Taxonomy

我为我的帖子设置了一个自定义分类法,名为$type. 如果$type == \'roast\'.<?php $number = 1; ?> <!-- the loop --> <?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?> <?php $type = get_the_term_list( $post->ID