我正在做一个无头wordpress,所以使用wordpress API。
使用Yoast,我将Yoast SEO数据添加到我的帖子类型API端点,如下所示:
function wp_api_encode_yoast($data, $post, $context) {
$yoastMeta = array(
\'yoast_wpseo_focuskw\' => get_post_meta($post->ID, \'_yoast_wpseo_focuskw\', true),
\'yoast_wpseo_title\' => wpseo_replace_vars(get_post_meta($post->ID, \'_yoast_wpseo_title\', true), $post),
\'yoast_wpseo_metadesc\' => wpseo_replace_vars(get_post_meta($post->ID, \'_yoast_wpseo_metadesc\', true), $post),
\'yoast_wpseo_linkdex\' => get_post_meta($post->ID, \'_yoast_wpseo_linkdex\', true),
\'yoast_wpseo_metakeywords\' => get_post_meta($post->ID, \'_yoast_wpseo_metakeywords\', true),
\'yoast_wpseo_meta_robots_noindex\' => get_post_meta($post->ID, \'_yoast_wpseo_meta-robots-noindex\', true),
\'yoast_wpseo_meta_robots_nofollow\' => get_post_meta($post->ID, \'_yoast_wpseo_meta-robots-nofollow\', true),
\'yoast_wpseo_meta_robots_adv\' => get_post_meta($post->ID, \'_yoast_wpseo_meta-robots-adv\', true),
\'yoast_wpseo_canonical\' => get_post_meta($post->ID, \'_yoast_wpseo_canonical\', true),
\'yoast_wpseo_redirect\' => get_post_meta($post->ID, \'_yoast_wpseo_redirect\', true),
\'yoast_wpseo_opengraph_title\' => get_post_meta($post->ID, \'_yoast_wpseo_opengraph-title\', true),
\'yoast_wpseo_opengraph_description\' => get_post_meta($post->ID, \'_yoast_wpseo_opengraph-description\', true),
\'yoast_wpseo_opengraph_image\' => get_post_meta($post->ID, \'_yoast_wpseo_opengraph-image\', true),
\'yoast_wpseo_twitter_title\' => get_post_meta($post->ID, \'_yoast_wpseo_twitter-title\', true),
\'yoast_wpseo_twitter_description\' => get_post_meta($post->ID, \'_yoast_wpseo_twitter-description\', true),
\'yoast_wpseo_twitter_image\' => get_post_meta($post->ID, \'_yoast_wpseo_twitter-image\', true)
);
$data->data[\'yoast_meta\'] = (array) $yoastMeta;
return $data;
}
add_filter(\'rest_prepare_post\', \'wp_api_encode_yoast\', 10, 3);
add_filter(\'rest_prepare_category\', \'wp_api_encode_yoast\', 10, 3);
add_filter(\'rest_prepare_page\', \'wp_api_encode_yoast\', 10, 3);
这适用于帖子、类别、页面和任何其他帖子类型。但我不知道如何将其添加到标记中,而且似乎缺少文档。我不相信
register_rest_field
是一个选项,因为它为特定的帖子类型注册了一个字段。。。我不认为标签被视为post类型。至少
rest_prepare_tag
过滤器不工作。
需要一些关于如何将rest字段添加到标记的指针。