为什么我无法从我的 post 类型中获取图库块以在我的 wordpress api 中显示
Why can't I get the gallery block from my post type to show in my wordpress api
在 Wordpress 5.1.1 中,我有一个名为 'food' 的自定义 post 类型。它由标题和图片库块组成。
在自定义 Wordpress rest API 端点中,我想从图库中获取图像 url。
在我发送数据之前,我循环遍历 post 并将 ACF 添加到 post 并且我认为相同的方法会起作用。
尝试过以下 Wordpress 功能:
get_post_gallery()
get_post_gallery_images()
我能理解的新方块方法。
get_post_galleries()
get_post_galleries_images()
这是我的 post 循环。 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);
}
这是我发给front-end的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&p=44",
"menu_order": 0,
"post_type": "food",
"post_mime_type": "",
"comment_count": "0",
"filter": "raw",
"acf": {
"post_template": "food"
},
"images": []
}
]
我可以通过查看 'post_content' 看到图片在那里,但找不到画廊。它一直在 'images'?
中给我一个空数组
有什么建议吗?
感谢阅读。
这些功能仅适用于经典编辑器中使用的(旧)[gallery]
短代码,因此它们不会 return 任何与图库 block (即 <!-- wp:gallery ... -->...<!-- /wp:gallery -->
):
get_post_gallery()
get_post_gallery_images()
get_post_galleries()
get_post_galleries_images()
而且我不知道画廊块的等效功能;但是,您可以使用此自定义函数来获得与 get_post_galleries_images()
将 return 类似的功能:(将其添加到主题 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=([\'"])(.+?)#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 );
希望这个回答对您和其他人有所帮助。 :)
在 Wordpress 5.1.1 中,我有一个名为 'food' 的自定义 post 类型。它由标题和图片库块组成。
在自定义 Wordpress rest API 端点中,我想从图库中获取图像 url。
在我发送数据之前,我循环遍历 post 并将 ACF 添加到 post 并且我认为相同的方法会起作用。
尝试过以下 Wordpress 功能:
get_post_gallery()
get_post_gallery_images()
我能理解的新方块方法。
get_post_galleries()
get_post_galleries_images()
这是我的 post 循环。 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);
}
这是我发给front-end的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&p=44",
"menu_order": 0,
"post_type": "food",
"post_mime_type": "",
"comment_count": "0",
"filter": "raw",
"acf": {
"post_template": "food"
},
"images": []
}
]
我可以通过查看 'post_content' 看到图片在那里,但找不到画廊。它一直在 'images'?
中给我一个空数组有什么建议吗? 感谢阅读。
这些功能仅适用于经典编辑器中使用的(旧)[gallery]
短代码,因此它们不会 return 任何与图库 block (即 <!-- wp:gallery ... -->...<!-- /wp:gallery -->
):
get_post_gallery()
get_post_gallery_images()
get_post_galleries()
get_post_galleries_images()
而且我不知道画廊块的等效功能;但是,您可以使用此自定义函数来获得与 get_post_galleries_images()
将 return 类似的功能:(将其添加到主题 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=([\'"])(.+?)#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 );
希望这个回答对您和其他人有所帮助。 :)