Php 在WordPress中为自定义帖子类型添加自定义标记

Php 在WordPress中为自定义帖子类型添加自定义标记,php,wordpress,custom-post-type,custom-taxonomy,custom-tag,Php,Wordpress,Custom Post Type,Custom Taxonomy,Custom Tag,我有许多自定义的文章类型,我想其中两个是由标签相关。这些标记仅可用于这两种自定义帖子类型 比如,;我有两个自定义的职位类型 书 作家 我希望这两个帖子类型有自定义标记,它只对这两个帖子类型有用 我该怎么做 我尝试使用自定义分类法,但没有成功。这是我使用的一个基本示例,您可以相应地进行更改 add_action( 'init', 'codex_book_init' ); /** * Register a book post type. * * @link http://codex.wordp

我有许多自定义的文章类型,我想其中两个是由标签相关。这些标记仅可用于这两种自定义帖子类型

比如,;我有两个自定义的职位类型

  • 作家
  • 我希望这两个帖子类型有自定义标记,它只对这两个帖子类型有用

    我该怎么做


    我尝试使用自定义分类法,但没有成功。

    这是我使用的一个基本示例,您可以相应地进行更改

    add_action( 'init', 'codex_book_init' );
    /**
     * Register a book post type.
     *
     * @link http://codex.wordpress.org/Function_Reference/register_post_type
     */
    function codex_book_init() {
        $labels = array(
            'name'               => _x( 'Books', 'post type general name', 'your-plugin-textdomain' ),
            'singular_name'      => _x( 'Book', 'post type singular name', 'your-plugin-textdomain' ),
            'menu_name'          => _x( 'Books', 'admin menu', 'your-plugin-textdomain' ),
            'name_admin_bar'     => _x( 'Book', 'add new on admin bar', 'your-plugin-textdomain' ),
            'add_new'            => _x( 'Add New', 'book', 'your-plugin-textdomain' ),
            'add_new_item'       => __( 'Add New Book', 'your-plugin-textdomain' ),
            'new_item'           => __( 'New Book', 'your-plugin-textdomain' ),
            'edit_item'          => __( 'Edit Book', 'your-plugin-textdomain' ),
            'view_item'          => __( 'View Book', 'your-plugin-textdomain' ),
            'all_items'          => __( 'All Books', 'your-plugin-textdomain' ),
            'search_items'       => __( 'Search Books', 'your-plugin-textdomain' ),
            'parent_item_colon'  => __( 'Parent Books:', 'your-plugin-textdomain' ),
            'not_found'          => __( 'No books found.', 'your-plugin-textdomain' ),
            'not_found_in_trash' => __( 'No books found in Trash.', 'your-plugin-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 );
    }
    
    // hook into the init action and call create_book_taxonomies when it fires
    add_action( 'init', 'create_book_taxonomies', 0 );
    
    // create two taxonomies, genres and writers for the post type "book"
    function create_book_taxonomies() {
        // Add new taxonomy, NOT hierarchical (like tags)
        $labels = array(
            'name'                       => _x( 'Categories', 'taxonomy general name' ),
            'singular_name'              => _x( 'Writer', 'Category singular name' ),
            'search_items'               => __( 'Search Category' ),
            'popular_items'              => __( 'Popular Category' ),
            'all_items'                  => __( 'All Writers' ),
            'parent_item'                => null,
            'parent_item_colon'          => null,
            'edit_item'                  => __( 'Edit Category' ),
            'update_item'                => __( 'Update Category' ),
            'add_new_item'               => __( 'Add New Category' ),
            'new_item_name'              => __( 'New Category Name' ),
            'separate_items_with_commas' => __( 'Separate Category with commas' ),
            'add_or_remove_items'        => __( 'Add or remove Category' ),
            'choose_from_most_used'      => __( 'Choose from the most used Category' ),
            'not_found'                  => __( 'No writers found.' ),
            'menu_name'                  => __( 'Category' ),
        );
    
        $args = array(
            'hierarchical'          => true,
            'labels'                => $labels,
            'show_ui'               => true,
            'show_admin_column'     => true,
            'update_count_callback' => '_update_post_term_count',
            'query_var'             => true,
            'rewrite'               => array( 'slug' => 'Category' ),
        );
    
        register_taxonomy( 'Category', 'book', $args );
    
        // Add new taxonomy, make it hierarchical (like categories)
        $labels = array(
            'name'              => _x( 'Tags', 'taxonomy general name' ),
            'singular_name'     => _x( 'Tags', 'taxonomy singular name' ),
            'search_items'      => __( 'Search Genres' ),
            'all_items'         => __( 'All Tags' ),
            'parent_item'       => __( 'Parent Tags' ),
            'parent_item_colon' => __( 'Parent Tags:' ),
            'edit_item'         => __( 'Edit Tags' ),
            'update_item'       => __( 'Update Tags' ),
            'add_new_item'      => __( 'Add New Tags' ),
            'new_item_name'     => __( 'New Tags Name' ),
            'menu_name'         => __( 'Tags' ),
        );
    
        $args = array(
            'hierarchical'      => false,
            'labels'            => $labels,
            'show_ui'           => true,
            'show_admin_column' => true,
            'query_var'         => true,
            'rewrite'           => array( 'slug' => 'Tags' ),
        );
    
        register_taxonomy( 'Tags', array( 'book' ), $args );
    
    
    }
    
    对于其他post类型,您可以使用相同的代码更改post类型和分类法,如

    register_post_type( 'writer', $args );
    register_taxonomy( 'Tags', array( 'writer' ), $args ); 
    

    这是我使用的一个基本示例,您可以相应地进行更改

    add_action( 'init', 'codex_book_init' );
    /**
     * Register a book post type.
     *
     * @link http://codex.wordpress.org/Function_Reference/register_post_type
     */
    function codex_book_init() {
        $labels = array(
            'name'               => _x( 'Books', 'post type general name', 'your-plugin-textdomain' ),
            'singular_name'      => _x( 'Book', 'post type singular name', 'your-plugin-textdomain' ),
            'menu_name'          => _x( 'Books', 'admin menu', 'your-plugin-textdomain' ),
            'name_admin_bar'     => _x( 'Book', 'add new on admin bar', 'your-plugin-textdomain' ),
            'add_new'            => _x( 'Add New', 'book', 'your-plugin-textdomain' ),
            'add_new_item'       => __( 'Add New Book', 'your-plugin-textdomain' ),
            'new_item'           => __( 'New Book', 'your-plugin-textdomain' ),
            'edit_item'          => __( 'Edit Book', 'your-plugin-textdomain' ),
            'view_item'          => __( 'View Book', 'your-plugin-textdomain' ),
            'all_items'          => __( 'All Books', 'your-plugin-textdomain' ),
            'search_items'       => __( 'Search Books', 'your-plugin-textdomain' ),
            'parent_item_colon'  => __( 'Parent Books:', 'your-plugin-textdomain' ),
            'not_found'          => __( 'No books found.', 'your-plugin-textdomain' ),
            'not_found_in_trash' => __( 'No books found in Trash.', 'your-plugin-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 );
    }
    
    // hook into the init action and call create_book_taxonomies when it fires
    add_action( 'init', 'create_book_taxonomies', 0 );
    
    // create two taxonomies, genres and writers for the post type "book"
    function create_book_taxonomies() {
        // Add new taxonomy, NOT hierarchical (like tags)
        $labels = array(
            'name'                       => _x( 'Categories', 'taxonomy general name' ),
            'singular_name'              => _x( 'Writer', 'Category singular name' ),
            'search_items'               => __( 'Search Category' ),
            'popular_items'              => __( 'Popular Category' ),
            'all_items'                  => __( 'All Writers' ),
            'parent_item'                => null,
            'parent_item_colon'          => null,
            'edit_item'                  => __( 'Edit Category' ),
            'update_item'                => __( 'Update Category' ),
            'add_new_item'               => __( 'Add New Category' ),
            'new_item_name'              => __( 'New Category Name' ),
            'separate_items_with_commas' => __( 'Separate Category with commas' ),
            'add_or_remove_items'        => __( 'Add or remove Category' ),
            'choose_from_most_used'      => __( 'Choose from the most used Category' ),
            'not_found'                  => __( 'No writers found.' ),
            'menu_name'                  => __( 'Category' ),
        );
    
        $args = array(
            'hierarchical'          => true,
            'labels'                => $labels,
            'show_ui'               => true,
            'show_admin_column'     => true,
            'update_count_callback' => '_update_post_term_count',
            'query_var'             => true,
            'rewrite'               => array( 'slug' => 'Category' ),
        );
    
        register_taxonomy( 'Category', 'book', $args );
    
        // Add new taxonomy, make it hierarchical (like categories)
        $labels = array(
            'name'              => _x( 'Tags', 'taxonomy general name' ),
            'singular_name'     => _x( 'Tags', 'taxonomy singular name' ),
            'search_items'      => __( 'Search Genres' ),
            'all_items'         => __( 'All Tags' ),
            'parent_item'       => __( 'Parent Tags' ),
            'parent_item_colon' => __( 'Parent Tags:' ),
            'edit_item'         => __( 'Edit Tags' ),
            'update_item'       => __( 'Update Tags' ),
            'add_new_item'      => __( 'Add New Tags' ),
            'new_item_name'     => __( 'New Tags Name' ),
            'menu_name'         => __( 'Tags' ),
        );
    
        $args = array(
            'hierarchical'      => false,
            'labels'            => $labels,
            'show_ui'           => true,
            'show_admin_column' => true,
            'query_var'         => true,
            'rewrite'           => array( 'slug' => 'Tags' ),
        );
    
        register_taxonomy( 'Tags', array( 'book' ), $args );
    
    
    }
    
    对于其他post类型,您可以使用相同的代码更改post类型和分类法,如

    register_post_type( 'writer', $args );
    register_taxonomy( 'Tags', array( 'writer' ), $args ); 
    

    这是我使用的一个基本示例,您可以相应地进行更改

    add_action( 'init', 'codex_book_init' );
    /**
     * Register a book post type.
     *
     * @link http://codex.wordpress.org/Function_Reference/register_post_type
     */
    function codex_book_init() {
        $labels = array(
            'name'               => _x( 'Books', 'post type general name', 'your-plugin-textdomain' ),
            'singular_name'      => _x( 'Book', 'post type singular name', 'your-plugin-textdomain' ),
            'menu_name'          => _x( 'Books', 'admin menu', 'your-plugin-textdomain' ),
            'name_admin_bar'     => _x( 'Book', 'add new on admin bar', 'your-plugin-textdomain' ),
            'add_new'            => _x( 'Add New', 'book', 'your-plugin-textdomain' ),
            'add_new_item'       => __( 'Add New Book', 'your-plugin-textdomain' ),
            'new_item'           => __( 'New Book', 'your-plugin-textdomain' ),
            'edit_item'          => __( 'Edit Book', 'your-plugin-textdomain' ),
            'view_item'          => __( 'View Book', 'your-plugin-textdomain' ),
            'all_items'          => __( 'All Books', 'your-plugin-textdomain' ),
            'search_items'       => __( 'Search Books', 'your-plugin-textdomain' ),
            'parent_item_colon'  => __( 'Parent Books:', 'your-plugin-textdomain' ),
            'not_found'          => __( 'No books found.', 'your-plugin-textdomain' ),
            'not_found_in_trash' => __( 'No books found in Trash.', 'your-plugin-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 );
    }
    
    // hook into the init action and call create_book_taxonomies when it fires
    add_action( 'init', 'create_book_taxonomies', 0 );
    
    // create two taxonomies, genres and writers for the post type "book"
    function create_book_taxonomies() {
        // Add new taxonomy, NOT hierarchical (like tags)
        $labels = array(
            'name'                       => _x( 'Categories', 'taxonomy general name' ),
            'singular_name'              => _x( 'Writer', 'Category singular name' ),
            'search_items'               => __( 'Search Category' ),
            'popular_items'              => __( 'Popular Category' ),
            'all_items'                  => __( 'All Writers' ),
            'parent_item'                => null,
            'parent_item_colon'          => null,
            'edit_item'                  => __( 'Edit Category' ),
            'update_item'                => __( 'Update Category' ),
            'add_new_item'               => __( 'Add New Category' ),
            'new_item_name'              => __( 'New Category Name' ),
            'separate_items_with_commas' => __( 'Separate Category with commas' ),
            'add_or_remove_items'        => __( 'Add or remove Category' ),
            'choose_from_most_used'      => __( 'Choose from the most used Category' ),
            'not_found'                  => __( 'No writers found.' ),
            'menu_name'                  => __( 'Category' ),
        );
    
        $args = array(
            'hierarchical'          => true,
            'labels'                => $labels,
            'show_ui'               => true,
            'show_admin_column'     => true,
            'update_count_callback' => '_update_post_term_count',
            'query_var'             => true,
            'rewrite'               => array( 'slug' => 'Category' ),
        );
    
        register_taxonomy( 'Category', 'book', $args );
    
        // Add new taxonomy, make it hierarchical (like categories)
        $labels = array(
            'name'              => _x( 'Tags', 'taxonomy general name' ),
            'singular_name'     => _x( 'Tags', 'taxonomy singular name' ),
            'search_items'      => __( 'Search Genres' ),
            'all_items'         => __( 'All Tags' ),
            'parent_item'       => __( 'Parent Tags' ),
            'parent_item_colon' => __( 'Parent Tags:' ),
            'edit_item'         => __( 'Edit Tags' ),
            'update_item'       => __( 'Update Tags' ),
            'add_new_item'      => __( 'Add New Tags' ),
            'new_item_name'     => __( 'New Tags Name' ),
            'menu_name'         => __( 'Tags' ),
        );
    
        $args = array(
            'hierarchical'      => false,
            'labels'            => $labels,
            'show_ui'           => true,
            'show_admin_column' => true,
            'query_var'         => true,
            'rewrite'           => array( 'slug' => 'Tags' ),
        );
    
        register_taxonomy( 'Tags', array( 'book' ), $args );
    
    
    }
    
    对于其他post类型,您可以使用相同的代码更改post类型和分类法,如

    register_post_type( 'writer', $args );
    register_taxonomy( 'Tags', array( 'writer' ), $args ); 
    

    这是我使用的一个基本示例,您可以相应地进行更改

    add_action( 'init', 'codex_book_init' );
    /**
     * Register a book post type.
     *
     * @link http://codex.wordpress.org/Function_Reference/register_post_type
     */
    function codex_book_init() {
        $labels = array(
            'name'               => _x( 'Books', 'post type general name', 'your-plugin-textdomain' ),
            'singular_name'      => _x( 'Book', 'post type singular name', 'your-plugin-textdomain' ),
            'menu_name'          => _x( 'Books', 'admin menu', 'your-plugin-textdomain' ),
            'name_admin_bar'     => _x( 'Book', 'add new on admin bar', 'your-plugin-textdomain' ),
            'add_new'            => _x( 'Add New', 'book', 'your-plugin-textdomain' ),
            'add_new_item'       => __( 'Add New Book', 'your-plugin-textdomain' ),
            'new_item'           => __( 'New Book', 'your-plugin-textdomain' ),
            'edit_item'          => __( 'Edit Book', 'your-plugin-textdomain' ),
            'view_item'          => __( 'View Book', 'your-plugin-textdomain' ),
            'all_items'          => __( 'All Books', 'your-plugin-textdomain' ),
            'search_items'       => __( 'Search Books', 'your-plugin-textdomain' ),
            'parent_item_colon'  => __( 'Parent Books:', 'your-plugin-textdomain' ),
            'not_found'          => __( 'No books found.', 'your-plugin-textdomain' ),
            'not_found_in_trash' => __( 'No books found in Trash.', 'your-plugin-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 );
    }
    
    // hook into the init action and call create_book_taxonomies when it fires
    add_action( 'init', 'create_book_taxonomies', 0 );
    
    // create two taxonomies, genres and writers for the post type "book"
    function create_book_taxonomies() {
        // Add new taxonomy, NOT hierarchical (like tags)
        $labels = array(
            'name'                       => _x( 'Categories', 'taxonomy general name' ),
            'singular_name'              => _x( 'Writer', 'Category singular name' ),
            'search_items'               => __( 'Search Category' ),
            'popular_items'              => __( 'Popular Category' ),
            'all_items'                  => __( 'All Writers' ),
            'parent_item'                => null,
            'parent_item_colon'          => null,
            'edit_item'                  => __( 'Edit Category' ),
            'update_item'                => __( 'Update Category' ),
            'add_new_item'               => __( 'Add New Category' ),
            'new_item_name'              => __( 'New Category Name' ),
            'separate_items_with_commas' => __( 'Separate Category with commas' ),
            'add_or_remove_items'        => __( 'Add or remove Category' ),
            'choose_from_most_used'      => __( 'Choose from the most used Category' ),
            'not_found'                  => __( 'No writers found.' ),
            'menu_name'                  => __( 'Category' ),
        );
    
        $args = array(
            'hierarchical'          => true,
            'labels'                => $labels,
            'show_ui'               => true,
            'show_admin_column'     => true,
            'update_count_callback' => '_update_post_term_count',
            'query_var'             => true,
            'rewrite'               => array( 'slug' => 'Category' ),
        );
    
        register_taxonomy( 'Category', 'book', $args );
    
        // Add new taxonomy, make it hierarchical (like categories)
        $labels = array(
            'name'              => _x( 'Tags', 'taxonomy general name' ),
            'singular_name'     => _x( 'Tags', 'taxonomy singular name' ),
            'search_items'      => __( 'Search Genres' ),
            'all_items'         => __( 'All Tags' ),
            'parent_item'       => __( 'Parent Tags' ),
            'parent_item_colon' => __( 'Parent Tags:' ),
            'edit_item'         => __( 'Edit Tags' ),
            'update_item'       => __( 'Update Tags' ),
            'add_new_item'      => __( 'Add New Tags' ),
            'new_item_name'     => __( 'New Tags Name' ),
            'menu_name'         => __( 'Tags' ),
        );
    
        $args = array(
            'hierarchical'      => false,
            'labels'            => $labels,
            'show_ui'           => true,
            'show_admin_column' => true,
            'query_var'         => true,
            'rewrite'           => array( 'slug' => 'Tags' ),
        );
    
        register_taxonomy( 'Tags', array( 'book' ), $args );
    
    
    }
    
    对于其他post类型,您可以使用相同的代码更改post类型和分类法,如

    register_post_type( 'writer', $args );
    register_taxonomy( 'Tags', array( 'writer' ), $args );