ソースを参照

:sparkles: including all image sizes in custom end point

tags/0.9.0
J 5年前
コミット
983f441aec
1個のファイルの変更22行の追加8行の削除
  1. 22
    8
      plugins/cia-endpoints/includes/class.make-endpoint.php

+ 22
- 8
plugins/cia-endpoints/includes/class.make-endpoint.php ファイルの表示

@@ -51,6 +51,25 @@ class Make_Endpoint_For extends WP_REST_Controller {
51 51
         return new WP_REST_Response( $this->prepare_item_for_response($args), 200 );
52 52
     }
53 53
 
54
+    private function _getAttachments($item) {
55
+        // Get media
56
+        $attached = array();
57
+            
58
+        // Image sizes you want
59
+        $desiredSizes = ['thumbnail', 'medium', 'large', 'full'];
60
+        
61
+        foreach ( get_attached_media( '', $item->ID ) as $attached_media ):
62
+            $imageSizes = array();
63
+            foreach ( $desiredSizes as $size ):
64
+                $imageSizes[$size] = wp_get_attachment_image_url($attached_media->ID, $size);
65
+            endforeach;
66
+            // store size:url map under image id
67
+            $attached[$attached_media->ID] = $imageSizes;
68
+        endforeach;
69
+
70
+        return $attached;
71
+    }
72
+
54 73
     public function prepare_item_for_response( $args ) {
55 74
         $collection = array();
56 75
         
@@ -63,16 +82,11 @@ class Make_Endpoint_For extends WP_REST_Controller {
63 82
                 parse_blocks( $item->post_content )
64 83
             );
65 84
 
66
-            // Get media
67
-            $media_ids = [];
68
-            foreach (get_attached_media( '', $item->ID ) as $attached_media):
69
-                array_push($media_ids, $attached_media->ID);
70
-            endforeach;
71
-            $filtered[attached] = $media_ids;
85
+            $filtered[attached] = $this->_getAttachments($item);
72 86
             
73 87
             // Galleries From blocks
74
-            $galleries = [];
75
-            foreach (get_attached_media( '', $item->ID ) as $attached_media):
88
+            $media_ids = array();
89
+            foreach ( get_attached_media( '', $item->ID ) as $attached_media ):
76 90
                 array_push($media_ids, $attached_media->ID);
77 91
             endforeach;
78 92
             $filtered[galleries] = get_ids_from_gallery_block(

読み込み中…
キャンセル
保存