为什么我无法从帖子类型中获取图库块以在wordpress api中显示

时间:2019-04-22 16:48:03

标签: wordpress rest api gallery

在Wordpress 5.1.1中,我具有名为“ food”的自定义帖子类型。它由标题和图片库块组成。

在自定义的Wordpress rest API端点中,我想从图库中获取图像URL。

在我发送数据之前,我先遍历帖子,然后将ACF添加到帖子中,以为可以使用相同的方法。

尝试了以下Wordpress功能:

get_post_gallery()
get_post_gallery_images()

据我了解,较新的块方法。

get_post_galleries()
get_post_galleries_images()

这是我的帖子循环。 ACF方法有效。

function api_get_all_posts() {
    $args = array(
        'post_type' => array(
           'food',
          ),
        'post_status' => 'publish',
        'order' => 'DESC',
        'post_per_page' => '5'
    );
    $the_query = new WP_Query($args);
    $posts = $the_query->posts;

    if ( empty( $posts ) ) {
        return new WP_Error(array( 'status' => 404 ) );
    } else {
      foreach ($posts as $key => $post) {
          $ID = $post->ID;
          //Set AFC
          $posts[$key]->acf = get_fields($ID);  
          //Set images
          $posts[$key]->images = get_post_galleries_images( $ID );
     }
   }   
  return rest_ensure_response($posts);
}

这是我发送到前端的JSON:

[
 {
    "ID": 44,
    "post_author": "0",
    "post_date": "2019-04-08 22:21:48",
    "post_date_gmt": "2019-04-08 22:21:48",
    "post_content": "<!-- wp:gallery {\"ids\":    [\"88\",\"87\",\"86\",91],\"columns\":4} -->\n<ul class=\"wp-block-gallery columns-4 is-cropped\"><li class=\"blocks-gallery-item\"><figure><img src=\"http://localhost/gwp/wp-content/uploads/2019/04/food-3-823x1024.jpg\" alt=\"\" data-id=\"88\" data-link=\"http://localhost/gwp/food-3/\" class=\"wp-    image-88\"/></figure></li></ul>\n<!-- /wp:gallery -->",
    "post_title": "Food test",
    "post_excerpt": "",
    "post_status": "publish",
    "comment_status": "closed",
    "ping_status": "closed",
    "post_password": "",
    "post_name": "fodd-test",
    "to_ping": "",
    "pinged": "",
    "post_modified": "2019-04-17 08:33:39",
    "post_modified_gmt": "2019-04-17 08:33:39",
    "post_content_filtered": "",
    "post_parent": 0,
    "guid": "http://localhost/gwp/?post_type=food&#038;p=44",
    "menu_order": 0,
    "post_type": "food",
    "post_mime_type": "",
    "comment_count": "0",
    "filter": "raw",
    "acf": {
        "post_template": "food"
    },
    "images": []
}
]

通过查看“ post_content”,我可以看到图像在那里,但是找不到图库。它会一直在“图像”中给我一个空数组?

有什么建议吗? 感谢您的阅读。

1 个答案:

答案 0 :(得分:1)

这些功能仅适用于经典编辑器中使用的(旧)[gallery]短代码,因此它们不会返回带有画廊 block 的任何内容(即<!-- wp:gallery ... -->...<!-- /wp:gallery -->):

  • get_post_gallery()
  • get_post_gallery_images()
  • get_post_galleries()
  • get_post_galleries_images()

我不知道Gallery块的等效功能;但是,您可以使用此自定义函数获得类似于get_post_galleries_images()返回的内容:(将此添加到主题functions.php文件中)

function get_post_block_galleries_images( $post_id ) {
    $content = get_post_field( 'post_content', $post_id );
    $srcs = [];

    $i = -1;
    foreach ( parse_blocks( $content ) as $block ) {
        if ( 'core/gallery' === $block['blockName'] ) {
            $i++;
            $srcs[ $i ] = [];

            preg_match_all( '#src=([\'"])(.+?)\1#is', $block['innerHTML'], $src, PREG_SET_ORDER );
            if ( ! empty( $src ) ) {
                foreach ( $src as $s ) {
                    $srcs[ $i ][] = $s[2];
                }
            }
        }
    }

    return $srcs;
}

然后在您的代码中,只需将其替换:

$posts[$key]->images = get_post_galleries_images( $ID );

具有:

$posts[$key]->images = get_post_block_galleries_images( $ID );

我希望这个答案对您和其他人有帮助。 :)