NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

class.make-terms.php 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. // include('formats.php');
  3. class Make_Terms_Endpoint extends WP_REST_Controller {
  4. /**
  5. * Register the routes for the objects of the controller.
  6. */
  7. public function register_custom_route($route) {
  8. $version = '2';
  9. $namespace = 'craft/v' . $version;
  10. register_rest_route( $namespace, '/' . $route, [
  11. array(
  12. 'methods' => WP_REST_Server::READABLE,
  13. 'callback' => array( $this, 'get_all_terms' )
  14. ),
  15. ]);
  16. }
  17. public function get_all_terms( $request ) {
  18. $terms = $categories = get_terms($post->post_type . '_type', array(
  19. 'orderby' => 'count',
  20. 'hide_empty' => 0,
  21. ));
  22. $taxonomies = get_object_taxonomies($post->post_type);
  23. $formatted[terms] = $terms;
  24. $formatted[taxonomies] = $taxonomies;
  25. return new WP_REST_Response( $this->prepare_all_items_for_response($args), 200 );
  26. }
  27. public function prepare_all_items_for_response( $args ) {
  28. $collection = [];
  29. // https://developer.wordpress.org/reference/functions/get_posts/
  30. foreach( get_posts($args) as $item ) {
  31. $formatted = default_post_format( $item, true );
  32. array_push($collection, $formatted);
  33. }
  34. wp_reset_postdata();
  35. return $collection;
  36. }
  37. }
  38. ?>