NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

formats.php 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. function make_taxonomy_endpoint_for( $terms ) {
  3. $type_slugs = [];
  4. foreach ($terms as $type_slug):
  5. array_push($type_slugs, $type_slug->slug);
  6. endforeach;
  7. return $type_slugs;
  8. }
  9. function default_post_format( $item, $include_content ) {
  10. $filtered = [];
  11. $filtered[id] = $item->ID;
  12. $filtered[slug] = $item->post_name;
  13. $filtered[type] = $item->post_type;
  14. $filtered[title] = $item->post_title;
  15. $filtered[excerpt] = $item->post_excerpt;
  16. $filtered[date] = $item->post_date;
  17. if(!$filtered[excerpt]) {
  18. $filtered[excerpt] = get_the_excerpt($item);
  19. }
  20. if($include_content) {
  21. $content = $item->post_content;
  22. $content = apply_filters('the_content', $content);
  23. $filtered[content] = str_replace(']]>', ']]&gt;', $content);
  24. }
  25. $filtered[featured] = get_the_post_thumbnail_url($item);
  26. if(!$filtered[featured]) {
  27. $thumbnailId = get_post_meta( $item->ID, '_thumbnail_id', true );
  28. $filtered[featured] = get_post($thumbnailId)->guid;
  29. }
  30. $filtered[thumb] = get_the_post_thumbnail_url($item, 'medium');
  31. if(!$filtered[thumb]) {
  32. $thumbnailId = get_post_meta( $item->ID, '_thumbnail_id', true );
  33. $filtered[thumb] = get_post($thumbnailId)->guid;
  34. }
  35. // Materials + type endpoints
  36. // if($item->post_type === 'artist' || $item->post_type === 'event' || $item->post_type === 'exhibition') {
  37. $posts_with_type = ['artist', 'exhbition', 'event'];
  38. if( in_array($item->post_type, $posts_with_type) ) {
  39. $filtered[materials] = make_taxonomy_endpoint_for( get_the_terms( $item, 'material' ) );
  40. $filtered[subtypes] = make_taxonomy_endpoint_for( get_the_terms( $item, $item->post_type . '_type' ) );
  41. }
  42. // Custom fields endpoint
  43. if($item->post_type === 'episode' && $include_content) $filtered[credits] = get_post_meta( $item->ID, 'credits', true );
  44. if($item->post_type === 'artist') $filtered[sortname] = get_post_meta( $item->ID, 'artist-sort-name', true );
  45. if($item->post_type === 'event') {
  46. $filtered[start] = get_post_meta( $item->ID, 'event-start-time', true );
  47. $filtered[end] = get_post_meta( $item->ID, 'event-end-time', true );
  48. $filtered[current] = time() <= (int)$filtered[end] && time() >= (int)$filtered[start];
  49. }
  50. if($item->post_type === 'exhibition') {
  51. $filtered[start] = get_post_meta( $item->ID, 'exhibit-start-date', true );
  52. $filtered[end] = get_post_meta( $item->ID, 'exhibit-end-date', true );
  53. $filtered[current] = time() <= (int)$filtered[end] && time() >= (int)$filtered[start];
  54. }
  55. // Post categories and tags (store just the slugs)
  56. if($item->post_type === 'post') {
  57. $filtered[categories] = make_taxonomy_endpoint_for(get_the_terms( $item, 'category' ));
  58. $filtered[tags] = make_taxonomy_endpoint_for(get_the_terms( $item, 'post_tag' ));
  59. }
  60. return $filtered;
  61. }
  62. ?>