几天来我一直在努力实现“相关帖子”功能,不幸的是 PHP/Wordpress 不是我的强项。
目前我正在我的前端点击这个端点:
posts?slug=${slug}&orderby=date&order=desc&categories=4&acf_format=standard
这将返回主要的 Post 对象,其中包含我期望的内容,例如 slug、status、sticky 等。在该对象中是 acf 对象,我可以在其中看到我的 related_posts 数组。我的问题源于这样一个事实,即这些 Post 对象以完全不同的字段呈现,主要以单词“post_”为前缀。
我在 acf-to-rest-api 插件上发现了这个 Github 问题,它与我面临的问题几乎相同(我什至偷了他们的问题标题!),但是到目前为止还无法实现它,它也是从 2017 年开始的,所以我不确定什么是过时的。
有没有办法返回正常的 WP Post JSON 格式?
function.php
功能向您的Wordpress主题add_filter()
添加过滤器。
在您的情况下,您可以检查 ACF 响应中是否存在
related_posts
字段。related_posts
数组映射到相应的 WP Post JSON 格式,并替换响应中的原始 related_posts
字段。
确保根据您的设置调整字段名称和附加字段(例如,如果您使用
Yoast SEO插件,
yoast_head
)。function modify_acf_to_rest_api_response($response, $request) {
if (isset($response->data['acf']['related_posts'])) {
$related_posts = $response->data['acf']['related_posts'];
// Map related_posts to their WP Post JSON format
$related_posts_json = array_map(function ($related_post) {
$post_data = get_post($related_post->ID);
$post_json = array(
'id' => $post_data->ID,
'date' => $post_data->post_date,
'date_gmt' => $post_data->post_date_gmt,
'guid' => $post_data->guid,
'modified' => $post_data->post_modified,
'modified_gmt' => $post_data->post_modified_gmt,
'slug' => $post_data->post_name,
'status' => $post_data->post_status,
'type' => $post_data->post_type,
'link' => get_permalink($post_data->ID),
'title' => array(
'rendered' => $post_data->post_title
),
'content' => array(
'rendered' => apply_filters('the_content', $post_data->post_content),
'protected' => false
),
'excerpt' => array(
'rendered' => apply_filters('the_excerpt', $post_data->post_excerpt),
'protected' => false
),
'author' => (int) $post_data->post_author,
'featured_media' => (int) get_post_thumbnail_id($post_data->ID),
'comment_status' => $post_data->comment_status,
'ping_status' => $post_data->ping_status,
'sticky' => false,
'template' => '',
'format' => get_post_format($post_data->ID),
'meta' => array(),
'categories' => wp_get_post_categories($post_data->ID),
'tags' => wp_get_post_tags($post_data->ID),
'yoast_head' => '', // If using Yoast SEO plugin
'_links' => array()
);
return $post_json;
}, $related_posts);
// Replace the related_posts with the WP Post JSON format
$response->data['acf']['related_posts'] = $related_posts_json;
}
return $response;
}
add_filter('rest_prepare_post', 'modify_acf_to_rest_api_response', 10, 2);
OP J. Jackson 添加评论:
我在 ACF 中使用标签选择而不是 WordPress 的标签。
我将如何用这些填充 Tags 数组?
这意味着您需要使用 ACF 标签填充
Tags
数组,方法是获取 ACF 标签字段值并将其包含在 $post_json
数组的标签键中。
我假设标签的 ACF 字段名称是
custom_tags
,但如果不同,请将“custom_tags
”替换为 ACF 标签字段的实际字段名称。
function modify_acf_to_rest_api_response($response, $request) {
if (isset($response->data['acf']['related_posts'])) {
$related_posts = $response->data['acf']['related_posts'];
// Map related_posts to their WP Post JSON format
$related_posts_json = array_map(function ($related_post) {
$post_data = get_post($related_post->ID);
$custom_tags = get_field('custom_tags', $related_post->ID); // Fetch ACF custom_tags field value
$post_json = array(
// ... other fields remain the same
'tags' => $custom_tags, // Replace this line with the ACF custom_tags field value
'_links' => array()
);
return $post_json;
}, $related_posts);
// Replace the related_posts with the WP Post JSON format
$response->data['acf']['related_posts'] = $related_posts_json;
}
return $response;
}
add_filter('rest_prepare_post', 'modify_acf_to_rest_api_response', 10, 2);