<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                REST API可以使用與默認帖子類型或分類術語控制器相同的控制器為wp / v2命名空間中的自定義帖子類型和自定義分類法創建路線。 或者,您可以使用自己的控制器和命名空間。 本文將介紹如何使用自定義內容類型的API路由的默認控制器。 這是最簡單的方法,并確保與第三方兼容的最高機會。 ##使用REST API支持注冊自定義帖子類型 注冊自定義帖子類型時,如果希望通過REST API可用,您應該在傳遞給register_post_type的參數中設置'show_in_rest'=> true。 將此參數設置為true將在wp / v2命名空間中添加路由。 ``` /** * Register a book post type, with REST API support * * Based on example at: https://codex.wordpress.org/Function_Reference/register_post_type */ add_action( 'init', 'my_book_cpt' ); function my_book_cpt() { $args = array( 'public' => true, 'show_in_rest' => true, 'label' => 'Books' ); register_post_type( 'book', $args ); } ``` 您可以選擇設置rest_base參數來更改基本URL,否則默認為該類型的名稱。 在下面的示例中,“books”用作rest_base的值。 這將使路由的URL為wp-json / v2 / books,而不是wp-json / v2 / book /,這將是默認的。 另外,您可以傳遞rest_controller_class的參數。 這個類必須是WP_REST_Controller的子類。 默認情況下,WP_REST_Posts_Controller用作控制器。 如果您使用的是自定義控制器,則可能不在wp / v2命名空間內。 下面是一個注冊一個post類型,一個完整的標簽,對REST API的支持,一個定制的rest_base以及默認控制器的顯式注冊表的例子: ``` /** * Register a book post type, with REST API support * * Based on example at: https://codex.wordpress.org/Function_Reference/register_post_type */ add_action( 'init', 'my_book_cpt' ); function my_book_cpt() { $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, 'description' => __( 'Description.', 'your-plugin-textdomain' ), '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, 'show_in_rest' => true, 'rest_base' => 'books', 'rest_controller_class' => 'WP_REST_Posts_Controller', 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ) ); register_post_type( 'book', $args ); } ``` ## 使用REST API支持注冊自定義分類法 使用REST API支持注冊自定義分類法與注冊自定義帖子類型非常相似:在傳遞給register_taxonomy的參數中傳遞'show_in_rest'=> true。 您可以選擇通過rest_base更改分類法路由的基本URL。 分類法的默認控制器是WP_REST_Terms_Controller。 如果您選擇使用自定義控制器,則可以使用rest_controller_class進行修改。 以下是使用REST API支持注冊自定義分類法的示例: ``` /** * Register a genre post type, with REST API support * * Based on example at: https://codex.wordpress.org/Function_Reference/register_taxonomy */ add_action( 'init', 'my_book_taxonomy', 30 ); function my_book_taxonomy() { $labels = array( 'name' => _x( 'Genres', 'taxonomy general name' ), 'singular_name' => _x( 'Genre', 'taxonomy singular name' ), 'search_items' => __( 'Search Genres' ), 'all_items' => __( 'All Genres' ), 'parent_item' => __( 'Parent Genre' ), 'parent_item_colon' => __( 'Parent Genre:' ), 'edit_item' => __( 'Edit Genre' ), 'update_item' => __( 'Update Genre' ), 'add_new_item' => __( 'Add New Genre' ), 'new_item_name' => __( 'New Genre Name' ), 'menu_name' => __( 'Genre' ), ); $args = array( 'hierarchical' => true, 'labels' => $labels, 'show_ui' => true, 'show_admin_column' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'genre' ), 'show_in_rest' => true, 'rest_base' => 'genre', 'rest_controller_class' => 'WP_REST_Terms_Controller', ); register_taxonomy( 'genre', array( 'book' ), $args ); } ``` ## 向現有內容類型添加REST API支持 當您不能控制的代碼(例如您正在使用的主題或插件)添加了自定義帖子類型或自定義分類法時,可能需要在已注冊alredy之后添加REST API支持。 參數與前面的例子相同,但需要添加到全局$ wp_post_types和$ wp_taxonomies數組中。 以下是向現有自定義帖子類型添加REST API支持的示例: ``` /** * Add REST API support to an already registered post type. */ add_action( 'init', 'my_custom_post_type_rest_support', 25 ); function my_custom_post_type_rest_support() { global $wp_post_types; //be sure to set this to the name of your post type! $post_type_name = 'planet'; if( isset( $wp_post_types[ $post_type_name ] ) ) { $wp_post_types[$post_type_name]->show_in_rest = true; // Optionally customize the rest_base or controller class $wp_post_types[$post_type_name]->rest_base = $post_type_name; $wp_post_types[$post_type_name]->rest_controller_class = 'WP_REST_Posts_Controller'; } } ``` 以下是如何向已注冊的自定義分類法添加REST API支持的示例。 ``` /** * Add REST API support to an already registered taxonomy. */ add_action( 'init', 'my_custom_taxonomy_rest_support', 25 ); function my_custom_taxonomy_rest_support() { global $wp_taxonomies; //be sure to set this to the name of your taxonomy! $taxonomy_name = 'planet_class'; if ( isset( $wp_taxonomies[ $taxonomy_name ] ) ) { $wp_taxonomies[ $taxonomy_name ]->show_in_rest = true; // Optionally customize the rest_base or controller class $wp_taxonomies[ $taxonomy_name ]->rest_base = $taxonomy_name; $wp_taxonomies[ $taxonomy_name ]->rest_controller_class = 'WP_REST_Terms_Controller'; } } ``` 如果您在執行這些示例中遇到問題,請確保您正在添加具有足夠高優先級的這些鉤子。 如果回調函數在發布類型或分類法被注冊之前運行,則isset檢查將阻止錯誤,但不會添加支持。 ##自定義鏈接關系 分類和自定義帖子類型在WordPress內部有一個內置關聯,但是如果要在兩個自定義帖子類型之間建立鏈接呢? 這在WordPress本身不正式支持,但是我們可以使用_link關系在任意內容類型之間創建我們自己的連接
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看