如果你需要用 WordPress 的 rest api 做小程序或者 APP,那你很大概率会用到 rest api,那么 WordPress 创建 taxonomy 自定义分类法时添加 rest api 支持呢?我们通常情况下你自己注册的 taxonomy 是并不能支持 rest api 访问的,你需要做额g r j i Y外的设置,下面是实例代码:
- /**
- * 注册一个有rest api~ g = Y a支持的taxonomy
- */
- add_action( 'init', 'my_b- O Jook_taxonomy', 30 );
- function; * - Z my_book_taxonomy(( c = 2 U f ,41; {
- $labeY P L N ` x { f Yls = array(
- 'name' => _x( 'GeC k 1nres', 'taxonomy general name' ),
- 'singular_name' => _x( 'Geu & fnre', 'v - S \ ? _ Qtaxonomy singu; 9 ! E ?lar name' ),
- 'search_items' => __( 'Search Genres' ),
- 'all_items' => __( 'All Genres' ),
- 'parent_item' => __( 'Parent Genre' &j r ^ d z#u \ W H41;,
- 'parent_item_colon' => __( 'Parent Genre:' ),
- 'edit_item' => __( 'Edit Genre' ),
- 'update_ite1 Z S ( p } ~ 3 tm' => __( 'Update Genre' ),
- 'add_new_item' => __( 'Add New Genre' ),
- 'new_item_name' => __( 'New Genre Name' ),
- 'menu_namH ? 2 m ) q pe' => __&9 0 ~#40; 'Genre' ),
- );
- $args = array(
- 'hierarchical' =&gi W \ {t; true,
- 'labels' => $labels,
- 'show_ui' => true,
- 'show_admin_column' => true,
- 'quS T - Yery_var' => true,
- 'rewrite6 } 2 N E \' => array( 'sl8 r H j 8 } $ 3 rug' => 'genre' ),
- 'show_in_rest' => true,
- 'rest_base' => 'genre',
- 'rest_controller_class' => 'WP_REST_Terms_Controller',
- U L d X Y D1;;
- register_taxonomy; @ U | v E w( 'genre', array( 'book' ), $argI r R k . Y n |s );
- }