如果你需要用 WordPress 的 rest api 做小程序或者 APP,那你很大概率会用到 rest api,那么 WordPress 创建 post type 自定义文章类型时添加 rest api 支持呢?通常情况下注册的 post type 是并不能支持 rest api 访问的,你需要做额外的设置。,s 3 o 6 e # O下面是实例代码:
- /**
- * 创建post type时添加rest api支持
- *
- */
- add_action( 'init', 'my_book_cpt' );
- function my_book_cp` w 0t() {
- $labels = array(
- 'name' => _x( 'BooksB r g o 7 7 2', 'post type general name', 'your-plugin-textdomain' ),
- 'singular_name' => _x( 'Book', 'post type singular name', 'your-plugin-textdomain' f @ ~ 9 # x T41;,
- 'menu_name' => _x( 'Books', 'admin menu', 'your-plugin-textdomain' A d +41;,
- 'name_admin_bar' => _x( 'Book', 'add new o3 7 gn admin bar', 'your-plugin-textdomain' ),
- 'add_new' => _x( 'Add New', 'book', 'yourH g W i i-plugin-textdomain' ),
- 'add_new_item' => __( 'Add New Book', 'your-plugin-textdomain' ),
- 'new_item' => _[ V W s x $ 4_( 'New Book', 'your-plug^ A [ % 3in-textdomain' &b h K P 2 _ I e#41;,
- 'edit_itemW t %' => __( 'Edit Book', 'your-plugin-textdomain' ),
- 'vieB - k o q * F u {w_item' => __( 'View Book', 'your-pluT & Y + F $ xgin-textdomain' ),
- 'all_items' => __( 'All Bo; l b aoks', 'your-plugin-textdomain' ),
- 'search_itemsB r o = x' =6 ~ Z M 7> __( 'Search Books', 'your-plugin-textdomain' ),
- 'parent_item_colon' => __4 c 1 a c a e ] 7( 'Parent Books:', 'your-plm | 0 M m _ y Kugin-textdomain' ),
- 'not_fo@ d | T 2 J N Kund' => __( 'No books found.', 'your-plugin-textdomain' ),
- 'not_found_in_trash' => __( 'No books found in Trash.', 'your-plugin-textdomain' )
- );
- $args = array(
- 'labels' => $labels,
- 'deZ V Mscription' => __( 'Description.', 'your-p= c Llugin-textdomain' ),o 2 K z H O
- 'public' => true,
- 'publicly_queryable'h A Y * v ( ? c N => true,
- 'show_ui' => true,
- 'show_in_menu' => true,
- 'query_var' => true,
- 'rewrite' => array( 'slug' => 'book' ),
- 'capabili! , r $ j + * o ?ty_type' => 'po; _ Ist',
- 'e L 7 ` = j Mhas_archive' => true,
- 'hierarchical' => false,
- 'menu_position' => nH ` J r i [ X Y wull,
- 'show_in_rest' => truew p B,
- 'rest_base' => 'books',
- 'rest_contr6 D ?oller_class' => 'WP_REST_Posts_ControlC 0 q B 4ler',
- 'supports' => array( 'title', 'editor', 'author'd 0 \ q ( P 1, 'thum+ + 3 R # ; ) # zbnail', 'excerpt', 'comments' )
- );
- register_post_type( 'bov 4 0 | \ok', $args );
- }